-
Notifications
You must be signed in to change notification settings - Fork 3.9k
binder: synchronize in-use stream updates #12458
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
Open
becomeStar
wants to merge
2
commits into
grpc:master
Choose a base branch
from
becomeStar:binder/sync-inuse-streams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,7 @@ | |
| import io.grpc.internal.StatsTraceContext; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.ScheduledFuture; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import javax.annotation.concurrent.ThreadSafe; | ||
|
|
||
|
|
@@ -73,7 +73,12 @@ public final class BinderClientTransport extends BinderTransport | |
| private final Bindable serviceBinding; | ||
|
|
||
| /** Number of ongoing calls which keep this transport "in-use". */ | ||
| private final AtomicInteger numInUseStreams; | ||
| @GuardedBy("this") | ||
| private int numInUseStreams; | ||
|
|
||
| /** Last in-use state that was reported to the listener */ | ||
| @GuardedBy("this") | ||
| private boolean listenerInUse; | ||
|
|
||
| private final long readyTimeoutMillis; | ||
| private final PingTracker pingTracker; | ||
|
|
@@ -114,7 +119,8 @@ public BinderClientTransport( | |
| Boolean preAuthServerOverride = options.getEagAttributes().get(PRE_AUTH_SERVER_OVERRIDE); | ||
| this.preAuthorizeServer = | ||
| preAuthServerOverride != null ? preAuthServerOverride : factory.preAuthorizeServers; | ||
| numInUseStreams = new AtomicInteger(); | ||
| numInUseStreams = 0; | ||
| listenerInUse = false; | ||
| pingTracker = new PingTracker(Ticker.systemTicker(), (id) -> sendPing(id)); | ||
|
|
||
| serviceBinding = | ||
|
|
@@ -259,9 +265,7 @@ public synchronized ClientStream newStream( | |
| return newFailingClientStream(failure, attributes, headers, tracers); | ||
| } | ||
|
|
||
| if (inbound.countsForInUse() && numInUseStreams.getAndIncrement() == 0) { | ||
| clientTransportListener.transportInUse(true); | ||
| } | ||
| updateInUseStreamsIfNeed(inbound.countsForInUse(), 1); | ||
| Outbound.ClientOutbound outbound = | ||
| new Outbound.ClientOutbound(this, callId, method, headers, statsTraceContext); | ||
| if (method.getType().clientSendsOneMessage()) { | ||
|
|
@@ -273,9 +277,7 @@ public synchronized ClientStream newStream( | |
|
|
||
| @Override | ||
| protected void unregisterInbound(Inbound<?> inbound) { | ||
| if (inbound.countsForInUse() && numInUseStreams.decrementAndGet() == 0) { | ||
| clientTransportListener.transportInUse(false); | ||
| } | ||
| updateInUseStreamsIfNeed(inbound.countsForInUse(), -1); | ||
| super.unregisterInbound(inbound); | ||
| } | ||
|
|
||
|
|
@@ -305,7 +307,9 @@ void notifyShutdown(Status status) { | |
| @Override | ||
| @GuardedBy("this") | ||
| void notifyTerminated() { | ||
| if (numInUseStreams.getAndSet(0) > 0) { | ||
| if(numInUseStreams > 0) { | ||
|
||
| numInUseStreams = 0; | ||
| listenerInUse = false; | ||
| clientTransportListener.transportInUse(false); | ||
| } | ||
| if (readyTimeoutFuture != null) { | ||
|
|
@@ -391,6 +395,25 @@ private synchronized void handleAuthResult(Throwable t) { | |
| Status.INTERNAL.withDescription("Could not evaluate SecurityPolicy").withCause(t), true); | ||
| } | ||
|
|
||
| /** Updates in-use-stream count and notifies listener only on transitions between 0 and >0 */ | ||
| private synchronized void updateInUseStreamsIfNeed(boolean countsForInUse, int delta) { | ||
| if(!countsForInUse) { | ||
| return; | ||
| } | ||
|
|
||
| numInUseStreams += delta; | ||
| if(numInUseStreams < 0) { | ||
| // Defensive: prevent negative due to unexpected double-decrement | ||
| numInUseStreams = 0; | ||
| } | ||
|
|
||
| boolean nowInUseStream = numInUseStreams > 0; | ||
| if(nowInUseStream != listenerInUse) { | ||
| listenerInUse = nowInUseStream; | ||
| clientTransportListener.transportInUse(nowInUseStream); | ||
| } | ||
| } | ||
|
|
||
| @GuardedBy("this") | ||
| @Override | ||
| protected void handlePingResponse(Parcel parcel) { | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see "A note on synchronization" at the top of BinderTransport.java. Note that
unregisterInbound()is called fromInbound.java's synchronizedcloseAbnormal()anddeliverSuffix()methods (viaunregister()) Is your proposed lock acquisition order safe or does it risk deadlock?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @jdcormie
I’ve updated the PR to address the potential deadlock you mentioned in “A note on synchronization” in BinderTransport.java.
I think the fix ensures listener notifications are executed outside the transport lock via the scheduled executor to maintain safe lock ordering.
I also updated the PR title and description to better reflect the changes, and formatted the code using google-java-format.
Could you please take a look when you have a moment? I’d really appreciate your review and feedback.