Skip to content

Fix crash: ThreadGuardedValue accessed after destroy in RunLoop race#2012

Open
hezhizhen wants to merge 1 commit intonikitabobko:mainfrom
hezhizhen:fix/thread-guarded-value-crash
Open

Fix crash: ThreadGuardedValue accessed after destroy in RunLoop race#2012
hezhizhen wants to merge 1 commit intonikitabobko:mainfrom
hezhizhen:fix/thread-guarded-value-crash

Conversation

@hezhizhen
Copy link
Contributor

When an app is destroyed, CFRunLoopStop is asynchronous — pending AX notification callbacks can still fire and access windows/axApp after destroy() has already set _threadGuarded to nil, causing a crash with "Value is already destroyed".

Add threadGuardedOrNil safe accessor to ThreadGuardedValue that returns nil instead of crashing, and use it in all RunLoop closures in MacApp so they gracefully bail out post-destroy.

Try to fix #1921

PR checklist

  • Explain your changes in the relevant commit messages rather than in the PR description. The PR description must not contain more information than the commit messages (except for images and other media).
  • Each commit must explain what/why/how and motivation in its description. https://cbea.ms/git-commit/
  • Don't forget to link the appropriate issues/discussions in commit messages (if applicable).
  • Each commit must be an atomic change (a PR may contain several commits). Don't introduce new functional changes together with refactorings in the same commit.
  • ./run-tests.sh exits with non-zero exit code.
  • Avoid merge commits, always rebase and force push.

Failure to follow the checklist with no apparent reasons will result in silent PR rejection.

When an app is destroyed, `CFRunLoopStop` is asynchronous — pending AX
notification callbacks can still fire and access `windows`/`axApp` after
`destroy()` has already set `_threadGuarded` to nil, causing a crash
with "Value is already destroyed".

Add `threadGuardedOrNil` safe accessor to `ThreadGuardedValue` that
returns nil instead of crashing, and use it in all RunLoop closures in
`MacApp` so they gracefully bail out post-destroy.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hezhizhen
Copy link
Contributor Author

@nikitabobko I know little about Swift, so I let claude code read the source code and error log and it created such a PR. Please check if it fixes the crash if it's okay for you to review an AI-automated PR.

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.

'Value is already destroyed' crash

1 participant