Skip to content

[Code scan] Avoid mutable defaults in Task and Resources #608

Description

@njzjz

Found by a Codex global repository scan of deepmodeling/dpdispatcher at commit 98a9e08.

Problem
Task and Resources constructors use mutable objects as default argument values and store caller-provided lists/dicts directly.

Relevant code

def __init__(
self,
command,
task_work_path,
forward_files=[],
backward_files=[],
outlog="log",
errlog="err",
):
self.command = command
self.task_work_path = task_work_path
self.forward_files = forward_files
self.backward_files = backward_files

def __init__(
self,
number_node,
cpu_per_node,
gpu_per_node,
queue_name,
group_size,
*,
custom_flags=[],
strategy=default_strategy,
para_deg=1,
module_unload_list=[],
module_purge=False,
module_list=[],
source_list=[],
envs={},
prepend_script=[],
append_script=[],
wait_time=0,
**kwargs,
):
self.number_node = number_node
self.cpu_per_node = cpu_per_node
self.gpu_per_node = gpu_per_node
self.queue_name = queue_name
self.group_size = group_size
# self.extra_specification = extra_specification
self.custom_flags = custom_flags
self.strategy = strategy
self.para_deg = para_deg
self.module_purge = module_purge
self.module_unload_list = module_unload_list
self.module_list = module_list
self.source_list = source_list
self.envs = envs
self.prepend_script = prepend_script
self.append_script = append_script

def zip_file_list(root_path, zip_filename, file_list=[]):
out_zip_file = os.path.join(root_path, zip_filename)
# print('debug: file_list', file_list)
zip_obj = ZipFile(out_zip_file, "w")
for f in file_list:
matched_files = os.path.join(root_path, f)
for ii in glob.glob(matched_files):

Impact
Mutating one object's forward_files, custom_flags, strategy, envs, or similar collection can affect later objects in the same Python process. Caller-owned lists and dicts can also be mutated after construction and change object behavior unexpectedly.

Suggested fix
Use None defaults and allocate fresh lists/dicts inside __init__. Copy caller-provided mutable collections before storing them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions