Skip to content

Commit ab458cc

Browse files
committed
api: document deadlock avoidance in ServerCall.triggerEvent and Listener.onEvent
Add deadlock avoidance notes to the Javadoc of ServerCall.triggerEvent() and ServerCall.Listener.onEvent(). Transports such as Binder may hold transport locks (e.g. Inbound.this) while synchronously dispatching triggerEvent and onEvent. If application or interceptor code holds internal locks when calling ServerCall methods or acquires them inside onEvent(), lock order inversion deadlocks can occur. TAG=agy CONV=b39523be-9572-456e-a428-8b0e255067da
1 parent 47b2131 commit ab458cc

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎api/src/main/java/io/grpc/ServerCall.java‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ public void onReady() {}
108108
* callbacks (like {@link #onMessage}, {@link #onHalfClose}). This means the implementation
109109
* does not need internal synchronization to access call-specific state.
110110
*
111+
* <p><strong>Deadlock avoidance:</strong> In some transports (such as Binder transport)
112+
* or when using a direct executor, this callback may be invoked while transport-level
113+
* locks are held. Implementations should avoid acquiring locks that are held by callers of
114+
* {@link ServerCall} methods (such as {@link ServerCall#triggerEvent},
115+
* {@link ServerCall#close}, or {@link ServerCall#request}), and should avoid calling
116+
* {@link ServerCall} methods while holding application-level locks, as this can lead to
117+
* deadlocks from lock-order inversion.
118+
*
111119
* @param event the triggered event.
112120
*/
113121
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/12979")
@@ -280,8 +288,19 @@ public String getAuthority() {
280288
* Triggers a custom event to be processed by the listener.
281289
* The event will be delivered to {@link Listener#onEvent(Object)} on the call's executor.
282290
*
283-
* <p>This method is thread-safe and can be called from any thread. No events will be delivered
284-
* after the RPC is cancelled or completed.
291+
* <p>This method is safe to call from multiple threads without external synchronization. No
292+
* events will be delivered after the RPC is cancelled or completed.
293+
*
294+
* <p><strong>Deadlock avoidance:</strong> Callers should avoid holding application-level or
295+
* interceptor locks when calling this method. Depending on the transport and executor
296+
* configuration (such as {@code directExecutor()} or transports like Binder),
297+
* {@code triggerEvent} may acquire transport-level locks and may dispatch
298+
* {@link Listener#onEvent(Object)} synchronously on the calling thread.
299+
* If the caller holds an application lock while calling {@code triggerEvent}, and
300+
* {@code onEvent} or a concurrent transport operation (such as {@link #close} or
301+
* {@link #request}) attempts to acquire that same lock, a deadlock can occur from
302+
* lock-order inversion. Applications should mutate internal state under lock, release
303+
* the lock, and only then invoke {@code triggerEvent}.
285304
*
286305
* @param event the event to trigger.
287306
*/

0 commit comments

Comments
 (0)