Skip to content

Fix mutable default arguments and resource leaks#44287

Merged
Rocketknight1 merged 2 commits intohuggingface:mainfrom
jashshah999:fix/mutable-defaults-and-resource-leaks
Mar 2, 2026
Merged

Fix mutable default arguments and resource leaks#44287
Rocketknight1 merged 2 commits intohuggingface:mainfrom
jashshah999:fix/mutable-defaults-and-resource-leaks

Conversation

@jashshah999
Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes mutable default arguments and unclosed file handles across several files.

Mutable defaults (can cause shared state across calls):

  • debug_utils.py: DebugUnderflowOverflow.__init__ trace_batch_nums=[] -> None
  • kernel_config.py: KernelConfig.__init__ kernel_mapping={} -> None
  • modeling_idefics.py: freeze_model, freeze_text_layers, freeze_vision_layers module_exceptions=[] -> ()
  • convert_usefulsensors_to_hf.py: _read_h5_weights weights={} -> None (this dict is actively mutated via assignment at line 50)

Resource leaks (file handles not closed):

  • processing_utils.py: open(template_file).read() in dict comprehension -> explicit loop with context manager
  • tokenization_myt5.py: json.load(open(vocab_file)) -> context manager
  • cli/serve.py: config_path.open().read() -> context manager

Who can review?

Anyone in the community is free to review the PR.

@Rocketknight1
Copy link
Copy Markdown
Member

Those file handles will be closed because they're never assigned to a variable, and so will immediately become unreachable and be cleaned up after the loop!

@jashshah999
Copy link
Copy Markdown
Contributor Author

Good point — reverted the file handle changes. The inline open().read() patterns are immediately unreachable and cleaned up by CPython's refcounting. Kept just the mutable default fixes.

Copy link
Copy Markdown
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems good, yes!

@Rocketknight1 Rocketknight1 force-pushed the fix/mutable-defaults-and-resource-leaks branch from bd8dd9d to a816980 Compare February 27, 2026 15:35
@Rocketknight1 Rocketknight1 enabled auto-merge (squash) February 27, 2026 15:39
@Rocketknight1
Copy link
Copy Markdown
Member

Hi @jashshah999 the processor failing test seems specific to this PR. Can you figure out what's going on?

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Mutable defaults (shared state across calls):
- debug_utils.py: DebugUnderflowOverflow trace_batch_nums=[] -> None
- kernel_config.py: KernelConfig kernel_mapping={} -> None
- modeling_idefics.py: freeze_model/freeze_text_layers/freeze_vision_layers
  module_exceptions=[] -> ()
- convert_usefulsensors_to_hf.py: _read_h5_weights weights={} -> None
  (actively mutated via dict assignment)

Resource leaks (file handles not closed):
- processing_utils.py: open().read() in chat template loading -> context manager
- tokenization_myt5.py: json.load(open()) -> context manager
- cli/serve.py: config_path.open().read() -> context manager
The open().read() patterns without variable assignment are
immediately unreachable and cleaned up by CPython's reference
counting GC, so context managers are unnecessary here.

Keeps the mutable default argument fixes which are real bugs.
auto-merge was automatically disabled March 1, 2026 06:51

Head branch was pushed to by a user without write access

@jashshah999 jashshah999 force-pushed the fix/mutable-defaults-and-resource-leaks branch from a816980 to efce737 Compare March 1, 2026 06:51
@jashshah999
Copy link
Copy Markdown
Contributor Author

I ran the idefics processor tests locally and all 22 pass. Rebased on latest main and force pushed -- the CI should re-run now. If it still fails, happy to dig deeper into which specific test is breaking.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 1, 2026

[For maintainers] Suggested jobs to run (before merge)

run-slow: idefics, moonshine

Copy link
Copy Markdown
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now!

@Rocketknight1 Rocketknight1 enabled auto-merge (squash) March 2, 2026 15:09
@Rocketknight1 Rocketknight1 merged commit c4dd818 into huggingface:main Mar 2, 2026
25 checks passed
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.

4 participants