Skip to content

Fix resource leaks, mutable default arguments, and duplicate line#183

Open
jashshah999 wants to merge 1 commit into
nvidia-cosmos:mainfrom
jashshah999:fix/resource-leaks-and-mutable-defaults
Open

Fix resource leaks, mutable default arguments, and duplicate line#183
jashshah999 wants to merge 1 commit into
nvidia-cosmos:mainfrom
jashshah999:fix/resource-leaks-and-mutable-defaults

Conversation

@jashshah999

Copy link
Copy Markdown

Description

Fixed resource leaks from unclosed file handles, mutable default arguments that can cause shared state mutations, and a duplicate line.

Problem

  1. Resource leaks: open(...).write(...) creates a file handle that is never explicitly closed, relying on garbage collection instead of deterministic cleanup.

  2. Mutable default arguments: Several functions use mutable dicts ({}) as default parameter values. In Python, mutable defaults are shared across all calls to the function. This is particularly problematic in dcp.py where mapping_keys is modified in-place via .update() — subsequent calls without an explicit argument would see the mutations from previous calls.

  3. Duplicate line: attention_mask = data_batch.get("padding_mask", None) appears twice consecutively in vlm_qwen_omni.py.

Solution

  • Wrapped open() calls with context managers (with statements) in inference.py and multiview.py.
  • Replaced mutable dict defaults with None + guard pattern across VLM models, checkpointer, and command IPC.
  • Removed the duplicate line in vlm_qwen_omni.py.

- Use context managers for file writes in `inference.py` and `multiview.py` to ensure file handles are properly closed.
- Replace mutable dict defaults (`data_batch={}`, `mapping_keys={}`, `result={}`) with `None` + guard in VLM models, checkpointer, and command IPC. Mutable defaults are shared across all calls and can cause unexpected state mutations (particularly in `dcp.py` where `mapping_keys` is modified via `.update()`).
- Remove duplicate `attention_mask = data_batch.get("padding_mask", None)` line in `vlm_qwen_omni.py`.

Signed-off-by: Jash Shah <jashshah.999@gmail.com>
@codeJRV codeJRV requested a review from asotoodeh-nv March 2, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant