Skip to content

Comments

Fix bare except clauses and mutable default arguments#8871

Merged
lstein merged 6 commits intoinvoke-ai:mainfrom
Mr-Neutr0n:fix/bare-except-and-mutable-defaults
Feb 23, 2026
Merged

Fix bare except clauses and mutable default arguments#8871
lstein merged 6 commits intoinvoke-ai:mainfrom
Mr-Neutr0n:fix/bare-except-and-mutable-defaults

Conversation

@Mr-Neutr0n
Copy link
Contributor

Summary

  • Replace bare except: with except Exception: in sqlite_database.py and mlsd/utils.py to avoid inadvertently catching KeyboardInterrupt and SystemExit, which can prevent graceful shutdowns and mask critical errors (PEP 8 E722).
  • Replace mutable default arguments ([]) with None in imwatermark/vendor.py (set_by_bits and EmbedMaxDct.__init__) to prevent shared state between calls — a well-known Python pitfall where default mutable objects persist across invocations and can cause subtle bugs if modified in place.

Changes

invokeai/app/services/shared/sqlite/sqlite_database.py

  • transaction() context manager: except:except Exception:

invokeai/backend/image_util/mlsd/utils.py

  • pred_squares(): three bare except:except Exception: (lines 572, 578, 586)

invokeai/backend/image_util/imwatermark/vendor.py

  • WatermarkEncoder.set_by_bits(bits=[])set_by_bits(bits=None) with None guard
  • EmbedMaxDct.__init__(watermarks=[], ..., scales=[0, 36, 36], ...)__init__(watermarks=None, ..., scales=None, ...) with None guards

Test plan

  • Verify invokeai imports and starts without errors
  • Verify MLSD control net processor works correctly
  • Verify watermark encoding/decoding still functions as expected
  • Verify database transactions commit and rollback correctly

Replace bare `except:` with `except Exception:` in sqlite_database.py
and mlsd/utils.py to avoid catching KeyboardInterrupt and SystemExit,
which can prevent graceful shutdowns and mask critical errors (PEP 8
E722).

Replace mutable default arguments (lists) with None in
imwatermark/vendor.py to prevent shared state between calls, which
is a known Python gotcha that can cause subtle bugs when default
mutable objects are modified in place.
@github-actions github-actions bot added python PRs that change python files backend PRs that change backend files services PRs that change app services labels Feb 11, 2026
@JPPhoto
Copy link
Collaborator

JPPhoto commented Feb 12, 2026

@Mr-Neutr0n Is there any way to add a test for this?

@Mr-Neutr0n
Copy link
Contributor Author

added a test for this

@github-actions github-actions bot added the python-tests PRs that change python tests label Feb 16, 2026
@JPPhoto
Copy link
Collaborator

JPPhoto commented Feb 18, 2026

added a test for this

@Mr-Neutr0n Thanks. This PR is failing a python check. Once you address that, I'll be happy to review it.

@lstein lstein added the v6.13.x label Feb 20, 2026
@lstein
Copy link
Collaborator

lstein commented Feb 20, 2026

@Mr-Neutr0n There are a couple of ruff errors showing up in the tests. Could you fix?

@lstein
Copy link
Collaborator

lstein commented Feb 21, 2026

@JPPhoto I went ahead and fixed the failing python check. I did a bit of testing on database rollback and didn't encounter any problems, but I didn't test the watermarking and MLSD control net functionality.

@JPPhoto JPPhoto linked an issue Feb 21, 2026 that may be closed by this pull request
@JPPhoto
Copy link
Collaborator

JPPhoto commented Feb 21, 2026

@lstein I think this is good. It doesn't solve the double ^C needed to exit Invoke but these changes make sense.

@lstein lstein merged commit ddaa12b into invoke-ai:main Feb 23, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend PRs that change backend files python PRs that change python files python-tests PRs that change python tests services PRs that change app services v6.13.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants