Fix race condition in ShutdownListener constructor#3158
Merged
jviau merged 1 commit intoOct 15, 2025
Conversation
The ShutdownListener constructor was registering the Cancel callback before assigning _innerListener. This created a race condition where if the shutdownToken fired during or immediately after registration, the Cancel() method would be called with _innerListener still null, causing a NullReferenceException. This fix assigns _innerListener before registering the callback, ensuring it's always initialized when Cancel() is invoked. Fixes Azure#2927
Contributor
Author
|
@jviau - could I trouble you to take a look at this PR as well? |
Contributor
|
/azp run webjobs-sdk.public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
jviau
approved these changes
Oct 15, 2025
Contributor
Author
|
Thanks @jviau! Appreciate it. |
This was referenced Dec 3, 2025
Closed
This was referenced Dec 29, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2927
This PR fixes a race condition in the
ShutdownListenerconstructor that causes aNullReferenceExceptionwhen the shutdown token fires during initialization.The Problem
The constructor was registering the cancellation callback before assigning
_innerListener:If the shutdown token fired in the microseconds between registering the callback and assigning
_innerListener, theCancel()method would be invoked with_innerListenerstill null, resulting in aNullReferenceExceptionat line 40.The Fix
This PR swaps the order to assign
_innerListenerbefore registering the callback:This ensures
_innerListeneris always initialized whenCancel()is invoked, eliminating the race condition.Testing
Impact
This is a critical fix for production stability. The race condition is rare but when it occurs, it causes the entire WebJob to crash during startup.