Skip to content

gh-79366: Fix a race condition when removing a logging handler - #154528

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:fix-logger-handler-race
Aug 2, 2026
Merged

gh-79366: Fix a race condition when removing a logging handler#154528
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:fix-logger-handler-race

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

removeHandler() removed the handler from the list in place. If a handler was removed while callHandlers() was iterating over the same list, whether from another thread or from within an emit() call, the following handlers of the same logger could be skipped, so records were not passed to them.

removeHandler() now replaces the list with a copy instead of mutating it in place, so callHandlers() always iterates over a consistent list without needing to acquire the lock. Adding a handler already appends to the end, so it cannot cause a handler to be skipped and is left unchanged.

🤖 Generated with Claude Code

removeHandler() mutated the handler list in place, so if a handler was
removed while callHandlers() was iterating the same list, the following
handlers could be skipped.  Replace the list instead of mutating it.

Co-Authored-By: Ben Spiller <11992588+ben-spiller@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@serhiy-storchaka
serhiy-storchaka merged commit 083e038 into python:main Aug 2, 2026
62 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@serhiy-storchaka
serhiy-storchaka deleted the fix-logger-handler-race branch August 2, 2026 10:16
@bedevere-app

bedevere-app Bot commented Aug 2, 2026

Copy link
Copy Markdown

GH-155076 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 2, 2026
@bedevere-app

bedevere-app Bot commented Aug 2, 2026

Copy link
Copy Markdown

GH-155077 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 2, 2026
@bedevere-app

bedevere-app Bot commented Aug 2, 2026

Copy link
Copy Markdown

GH-155078 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Aug 2, 2026
serhiy-storchaka added a commit that referenced this pull request Aug 2, 2026
…GH-154528) (GH-155078)

removeHandler() mutated the handler list in place, so if a handler was
removed while callHandlers() was iterating the same list, the following
handlers could be skipped.  Replace the list instead of mutating it.
(cherry picked from commit 083e038)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Ben Spiller <11992588+ben-spiller@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
serhiy-storchaka added a commit that referenced this pull request Aug 2, 2026
…GH-154528) (GH-155077)

removeHandler() mutated the handler list in place, so if a handler was
removed while callHandlers() was iterating the same list, the following
handlers could be skipped.  Replace the list instead of mutating it.
(cherry picked from commit 083e038)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Ben Spiller <11992588+ben-spiller@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
serhiy-storchaka added a commit that referenced this pull request Aug 2, 2026
…GH-154528) (GH-155076)

removeHandler() mutated the handler list in place, so if a handler was
removed while callHandlers() was iterating the same list, the following
handlers could be skipped.  Replace the list instead of mutating it.
(cherry picked from commit 083e038)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Ben Spiller <11992588+ben-spiller@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

2 participants