Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Made ServerImpl.internalClose thread-safe. #11864

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

harshagoo94
Copy link
Contributor

core: Added changes to make ServerImpl.internalClose thread-safe and trigger cancel instead of completed.

Fixes #3746.

@harshagoo94 harshagoo94 marked this pull request as draft January 31, 2025 09:01
@harshagoo94 harshagoo94 marked this pull request as ready for review February 5, 2025 13:07
@harshagoo94 harshagoo94 marked this pull request as draft February 5, 2025 13:08
@harshagoo94
Copy link
Contributor Author

@shivaspeaks

Note:

We are not following cancel calls as 2nd option (since option 2 is not feasible). Instead, we are allowing truncated messages in the stream and delivering the trailers.

@harshagoo94 harshagoo94 marked this pull request as ready for review February 5, 2025 15:25
@@ -503,7 +503,7 @@ private void streamCreatedInternal(

final JumpToApplicationThreadServerStreamListener jumpListener
= new JumpToApplicationThreadServerStreamListener(
wrappedExecutor, executor, stream, context, tag);
wrappedExecutor, executor, stream, context, tag, headers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to create the trailers with metadata from the exception caught. See example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that using these headers is very wrong, as echoing back the client's request headers is harmful. Although, in fact, the original code was fine and it should just be a new set of Metadata. None of the callers of internalClose() has metadata attached, and even if they do, we'd need to understand why a bit better because sending RST_STREAM was a valid way to handle this, which won't have metadata.

@@ -503,7 +503,7 @@ private void streamCreatedInternal(

final JumpToApplicationThreadServerStreamListener jumpListener
= new JumpToApplicationThreadServerStreamListener(
wrappedExecutor, executor, stream, context, tag);
wrappedExecutor, executor, stream, context, tag, headers);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that using these headers is very wrong, as echoing back the client's request headers is harmful. Although, in fact, the original code was fine and it should just be a new set of Metadata. None of the callers of internalClose() has metadata attached, and even if they do, we'd need to understand why a bit better because sending RST_STREAM was a valid way to handle this, which won't have metadata.

@@ -808,10 +824,9 @@ void setListener(ServerStreamListener listener) {
/**
* Like {@link ServerCall#close(Status, Metadata)}, but thread-safe for internal use.
*/
private void internalClose(Throwable t) {
// TODO(ejona86): this is not thread-safe :)
private synchronized void internalClose(Throwable t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing synchronized on it doesn't make it thread-safe. You'd have to synchronize most calls to the stream, and make sure to stop calling the stream after closing it. And we don't want to synchronize most calls to the stream. We will need help from the stream to implement this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all 3 callers of ServerImpl.closeInternal viz., onReady, messagesAvailable and halfClosed run serialized on the callExecutor, what makes the stream.close call in internalClose non-thread safe? Is the race with other methods on the stream?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onReady, messagesAvailable, and halfClosed are callbacks and don't generally call into stream (other than internalClose()). You need to look for other calls into stream. That would be from ServerCallImpl which is called by the application on arbitrary threads (but only one thread concurrently).

This is the "three threads" we had talked about for RPCs: application thread, transport thread, callback thread. stream is used on the application thread, yet here we are using it from the callback thread (callExecutor).

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.

Make ServerImpl's internalClose thread-safe
3 participants