Repository navigation
xds: resolve onError / channel teardown lock ordering conflict - #13089
Closed
bcleenders wants to merge 1 commit into
Closed
bcleenders wants to merge 1 commit into
bcleenders wants to merge 1 commit into
Conversation
`onError` is the only notification path in `DynamicSslContextProvider` that
bypasses `performCallback` and calls `callback.onException()` directly on
the caller's thread. Other update paths (`updateSslContext` / `addCallback`)
already dispatch through `performCallback`.
Staying on the same thread holds on to `DistributorWatcher` lock too long, and
creates a lock-ordering cycle when a `file_watcher` cert provider fails to
reload certificates:
```
Thread A (cert reload):
DistributorWatcher.onError() — holds DistributorWatcher lock
→ DynamicSslContextProvider.onError()
→ callback.onException() — synchronous, still holds lock, we break this
→ releaseSslContextProvider()
→ ReferenceCountingMap.releaseInternal() — wants map lock
Thread B (channel teardown):
ReferenceCountingMap.releaseInternal() — holds map lock
→ value.close()
→ Handle.close()
→ DistributorWatcher.removeWatcher() — wants DistributorWatcher lock
```
The fix routes `onError` through `performCallback`, matching the update
path. The callback runs on its own executor after the caller releases the
DistributorWatcher lock, eliminating the lock-ordering edge.
bcleenders
marked this pull request as ready for review
October 5, 2026 13:11
bcleenders
added a commit
to bcleenders/grpc-java
that referenced
this pull request
Oct 5, 2026
Reviewer feedback: instead of holding the intrinsic lock during watcher callbacks (and using `ImmutableSet.copyOf` to tolerate reentrant modification), decouple state mutation from callback delivery using `SynchronizationContext`. Under the intrinsic lock, state is updated and callbacks are enqueued via `executeLater`. After releasing the lock, `drain()` delivers the callbacks. This guarantees: - No lock held during callbacks (prevents deadlocks with downstream locks such as `ReferenceCountingMap`) - Serialized, non-reentrant delivery (a callback that calls `addWatcher` enqueues work that runs after the current callback) This also subsumes the `DynamicSslContextProvider.onError` deadlock fix from grpc#13089: the `DistributorWatcher` lock is no longer held when `onError` callbacks run, so the lock-ordering cycle between `DistributorWatcher` and `ReferenceCountingMap` cannot occur. Exceptions from watcher callbacks no longer propagate to the caller (`SynchronizationContext` catches and logs them). The only caller that relied on propagation is `FileWatcherCertificateProvider`, which skips a `lastModifiedTime` update when `updateTrustedRoots` throws, causing a retry on the next refresh cycle. This retry was ineffective: the same cert data produces the same deterministic failure (e.g., a static `CertificateValidationContext` rejecting the cert), so retrying just repeats the same error until the file changes on disk. The error is still delivered to the affected watcher via `DynamicSslContextProvider.onError()`. Co-Authored-By: Claude Oratorio 5.0 <noreply@anthropic.com>
bcleenders
added a commit
to bcleenders/grpc-java
that referenced
this pull request
Oct 5, 2026
Reviewer feedback: instead of holding the intrinsic lock during watcher callbacks (and using `ImmutableSet.copyOf` to tolerate reentrant modification), decouple state mutation from callback delivery using `SynchronizationContext`. Under the intrinsic lock, state is updated and callbacks are enqueued via `executeLater`. After releasing the lock, `drain()` delivers the callbacks. This guarantees: - No lock held during callbacks (prevents deadlocks with downstream locks such as `ReferenceCountingMap`) - Serialized, non-reentrant delivery (a callback that calls `addWatcher` enqueues work that runs after the current callback) This also subsumes the `DynamicSslContextProvider.onError` deadlock fix from grpc#13089: the `DistributorWatcher` lock is no longer held when `onError` callbacks run, so the lock-ordering cycle between `DistributorWatcher` and `ReferenceCountingMap` cannot occur. Exceptions from watcher callbacks no longer propagate to the caller (`SynchronizationContext` catches and logs them). The only caller that relied on propagation is `FileWatcherCertificateProvider`, which skips a `lastModifiedTime` update when `updateTrustedRoots` throws, causing a retry on the next refresh cycle. This retry was ineffective: the same cert data produces the same deterministic failure (e.g., a static `CertificateValidationContext` rejecting the cert), so retrying just repeats the same error until the file changes on disk. The error is still delivered to the affected watcher via `DynamicSslContextProvider.onError()`.
bcleenders
added a commit
to bcleenders/grpc-java
that referenced
this pull request
Oct 5, 2026
Reviewer feedback: instead of holding the intrinsic lock during watcher callbacks (and using `ImmutableSet.copyOf` to tolerate reentrant modification), decouple state mutation from callback delivery using `SynchronizationContext`. Under the intrinsic lock, state is updated and callbacks are enqueued via `executeLater`. After releasing the lock, `drain()` delivers the callbacks. This guarantees: - No lock held during callbacks (prevents deadlocks with downstream locks such as `ReferenceCountingMap`) - Serialized, non-reentrant delivery (a callback that calls `addWatcher` enqueues work that runs after the current callback) This also subsumes the `DynamicSslContextProvider.onError` deadlock fix from grpc#13089: the `DistributorWatcher` lock is no longer held when `onError` callbacks run, so the lock-ordering cycle between `DistributorWatcher` and `ReferenceCountingMap` cannot occur. Exceptions from watcher callbacks no longer propagate to the caller (`SynchronizationContext` catches and logs them). The only caller that relied on propagation is `FileWatcherCertificateProvider`, which skips a `lastModifiedTime` update when `updateTrustedRoots` throws, causing a retry on the next refresh cycle. This retry was ineffective: the same cert data produces the same deterministic failure (e.g., a static `CertificateValidationContext` rejecting the cert), so retrying just repeats the same error until the file changes on disk. The error is still delivered to the affected watcher via `DynamicSslContextProvider.onError()`.
Contributor
Author
|
Addressed by fc04aa5 |
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.
onErroris the only notification path inDynamicSslContextProviderthatbypasses
performCallbackand callscallback.onException()directly onthe caller's thread. Other update paths (
updateSslContext/addCallback)already dispatch through
performCallback.Staying on the same thread holds on to
DistributorWatcherlock too long, andcreates a lock-ordering cycle when a
file_watchercert provider fails toreload certificates:
The fix routes
onErrorthroughperformCallback, matching the updatepath. The callback runs on its own executor after the caller releases the
DistributorWatcher lock, eliminating the lock-ordering edge.