Skip to content

Repo File Sync: Update DocBuild File is not always closed#241

Open
odaysec wants to merge 1 commit intomicrosoft:masterfrom
odaysec:patch-1
Open

Repo File Sync: Update DocBuild File is not always closed#241
odaysec wants to merge 1 commit intomicrosoft:masterfrom
odaysec:patch-1

Conversation

@odaysec
Copy link

@odaysec odaysec commented Mar 8, 2026

Fix issue problem the file opens in MakeYml should be wrapped in with context managers so they are always closed, even if an exception occurs. We still need to preserve current behavior: copy all lines from the base YAML file into the output file, then parse the base YAML from the beginning to check for an extra key, raise if present, and finally keep an open handle to the output YAML in self.Yml for later use by CloseYml.

The safest change is:

  • Open self.YmlFilePathBase with with open(..., 'r') as ymlbase: so that it is always closed after use.
  • Within that with, open self.YmlFilePathOut with another with open(..., 'w') as yamlout: and perform the line copy and YAML parse while both files are open and automatically managed.
  • After the nested with blocks, reopen self.YmlFilePathOut in append mode ('a') and assign that handle to self.Yml. This preserves the design where self.Yml stays open beyond MakeYml and will be closed in CloseYml, without risking leaks if an exception occurs during the initial processing.
  • Remove the explicit ymlbase.close() and the assignment self.Yml = yamlout inside MakeYml, since those handles are now managed by context managers and a fresh handle is created for self.Yml.

Reading and writing files
The with statement, The try statement
Python PEP 343 The "with" Statement
CWE-772

Fix issue problem the file opens in `MakeYml` should be wrapped in `with` context managers so they are always closed, even if an exception occurs. We still need to preserve current behavior: copy all lines from the base YAML file into the output file, then parse the base YAML from the beginning to check for an `extra` key, raise if present, and finally keep an open handle to the output YAML in `self.Yml` for later use by `CloseYml`.

The safest change is:
- Open `self.YmlFilePathBase` with `with open(..., 'r') as ymlbase:` so that it is always closed after use.
- Within that `with`, open `self.YmlFilePathOut` with another `with open(..., 'w') as yamlout:` and perform the line copy and YAML parse while both files are open and automatically managed.
- After the nested `with` blocks, reopen `self.YmlFilePathOut` in append mode (`'a'`) and assign that handle to `self.Yml`. This preserves the design where `self.Yml` stays open beyond `MakeYml` and will be closed in `CloseYml`, without risking leaks if an exception occurs during the initial processing.
- Remove the explicit `ymlbase.close()` and the assignment `self.Yml = yamlout` inside `MakeYml`, since those handles are now managed by context managers and a fresh handle is created for `self.Yml`.
@mu-automation mu-automation bot added the language:python Pull requests that update Python code label Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

language:python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant