-
Notifications
You must be signed in to change notification settings - Fork 184
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
ProxyConnectConnectionFactoryFilter
leaks connection in case of errors
#1002
ProxyConnectConnectionFactoryFilter
leaks connection in case of errors
#1002
Conversation
Motivation: In case of any error or cancellation, `ProxyConnectConnectionFactoryFilter` does not close the connection to the proxy. Modifications: - Add tests to verify different failure scenarios for `ProxyConnectConnectionFactoryFilter`; - Close connection when `CONNECT` request or SSL handshake failures or if a `Single` is cancelled; Result: `ProxyConnectConnectionFactoryFilter` does not leak connections.
...-http-netty/src/main/java/io/servicetalk/http/netty/ProxyConnectConnectionFactoryFilter.java
Outdated
Show resolved
Hide resolved
...-http-netty/src/main/java/io/servicetalk/http/netty/ProxyConnectConnectionFactoryFilter.java
Outdated
Show resolved
Hide resolved
...-http-netty/src/main/java/io/servicetalk/http/netty/ProxyConnectConnectionFactoryFilter.java
Outdated
Show resolved
Hide resolved
...-http-netty/src/main/java/io/servicetalk/http/netty/ProxyConnectConnectionFactoryFilter.java
Outdated
Show resolved
Hide resolved
...-http-netty/src/main/java/io/servicetalk/http/netty/ProxyConnectConnectionFactoryFilter.java
Outdated
Show resolved
Hide resolved
// Close recently created connection in case of any error while it connects to the proxy | ||
// or cancellation: | ||
.recoverWith(t -> c.closeAsync().concat(failed(t))) | ||
.whenCancel(() -> c.closeAsync().subscribe()); |
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.
we really do not know if the cancel has come due to the operation was canceled by the user or due to some operators sending a cancel for the previous source when they move on to the next source (eg:
concat()
).
Can any operator cancel after success? IIUC they cancel the previous source only for non-success/non-complete cases.
LMK if I need to revert whenFinally
here to prevent closure on cancel after success.
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.
Can any operator cancel after success?
Yes they do, consider this connFactory.newConnection().concat(executor.timer(1, MILLISECONDS)
(eg: to add a delay to respond to connect)
concat()
uses SequentialCancellable
which cancels the old Cancellable
when the new Cancellable
is received, which in this case will be after the successful completion of connFactory.newConnection()
.
More generally, we should not assume anywhere that cancel
is only received before success()
as Cancellable
and Subscriber
code paths are concurrent.
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.
Looking at SequentialCancellable
and TBH don't see where it cancels the old Cancellable
. When the new Cancellable
is received it may close the new one immediately if the oldVal
was already canceled via SequentialCancellable#cancel()
.
More generally, we should not assume anywhere that
cancel
is only received beforesuccess()
asCancellable
andSubscriber
code paths are concurrent.
Agreed. I just thought that it doesn't matter when proxy filter sees cancel: before or after onSuccess we should close the connection if we saw that someone is not interested in the result anymore.
Btw, after #1005, should it be afterCancel
or afterFinally
?
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.
whenCancel
will unconditionally execute the callback when cancel
is called regardless if the connection has been delivered downstream. If we have already delivered the connection
we shouldn't later close it (regardless if someone cancels or not). In addition to this being the expected control flow, the RS spec has some rules which discuss cancel being a no-op after a terminal signal is delivered [1][2].
afterFinally(SingleTerminalSignalConsumer<T> doFinally)
happens to enforce "only a single callback will be executed" but may still result in invoking the onCancel()
call back and also calling the downstream Subscriber#onSuccess(...)
for the following reasons:
- Subscription can be invoked on a different thread
- Data/terminal signals may still be delivered after
cancel
[3]
So afterFinally
is an improvement over afterCancel
, but still isn't ideal because we may deliver a closed object (and/or invoke closeAsync()
concurrently).
[1] https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#1.6
If a Publisher signals either onError or onComplete on a Subscriber, that Subscriber’s Subscription MUST be considered cancelled.
[2] https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#3.7
After the Subscription is cancelled, additional Subscription.cancel() MUST be NOPs.
[3] https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#2.8
A Subscriber MUST be prepared to receive one or more onNext signals after having called Subscription.cancel() if there are still requested elements pending [see 3.12]. Subscription.cancel() does not guarantee to perform the underlying cleaning operations immediately.
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.
TBH don't see where it cancels the old Cancellable.
Aah, you are correct. I misread under an older assumption that we cancel()
the previous Cancellable
.
Anyways, for other reasons me and Scott mention, unconditional close()
upon cancel()
isn't correct.
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.
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.
So afterFinally is an improvement over afterCancel, but still isn't ideal because we may deliver a closed object (and/or invoke closeAsync() concurrently).
Ok ya this seems to be a problem. Can we remove the close-on-cancel part for now?
Connection lifetime is anyways a problem in such situation out of the context of this filter as mentioned in #1002 (comment).
Lets fix the obvious issue of leaking connection for non-200 responses and then handle lifecycle on cancel/early termination 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.
} | ||
}); | ||
private Single<C> handleConnectResponse(final C connection, final StreamingHttpResponse response) { | ||
try { |
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.
Moved this logic to the different method because nested try
blocks make code indentation awful.
// Close recently created connection in case of any error while it connects to the proxy | ||
// or cancellation: | ||
.recoverWith(t -> c.closeAsync().concat(failed(t))) | ||
.whenCancel(() -> c.closeAsync().subscribe()); |
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.
whenCancel
will unconditionally execute the callback when cancel
is called regardless if the connection has been delivered downstream. If we have already delivered the connection
we shouldn't later close it (regardless if someone cancels or not). In addition to this being the expected control flow, the RS spec has some rules which discuss cancel being a no-op after a terminal signal is delivered [1][2].
afterFinally(SingleTerminalSignalConsumer<T> doFinally)
happens to enforce "only a single callback will be executed" but may still result in invoking the onCancel()
call back and also calling the downstream Subscriber#onSuccess(...)
for the following reasons:
- Subscription can be invoked on a different thread
- Data/terminal signals may still be delivered after
cancel
[3]
So afterFinally
is an improvement over afterCancel
, but still isn't ideal because we may deliver a closed object (and/or invoke closeAsync()
concurrently).
[1] https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#1.6
If a Publisher signals either onError or onComplete on a Subscriber, that Subscriber’s Subscription MUST be considered cancelled.
[2] https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#3.7
After the Subscription is cancelled, additional Subscription.cancel() MUST be NOPs.
[3] https://github.com/reactive-streams/reactive-streams-jvm/blob/v1.0.3/README.md#2.8
A Subscriber MUST be prepared to receive one or more onNext signals after having called Subscription.cancel() if there are still requested elements pending [see 3.12]. Subscription.cancel() does not guarantee to perform the underlying cleaning operations immediately.
...-http-netty/src/main/java/io/servicetalk/http/netty/ProxyConnectConnectionFactoryFilter.java
Outdated
Show resolved
Hide resolved
Build failed because of an issue with docker. @servicetalk-bot test this please |
jdk8 build failed with timeout @servicetalk-bot test this please |
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.
LGTM after these comments
...-http-netty/src/main/java/io/servicetalk/http/netty/ProxyConnectConnectionFactoryFilter.java
Outdated
Show resolved
Hide resolved
.flatMap(response -> handleConnectResponse(c, response)) | ||
// Close recently created connection in case of any error while it connects to the proxy: | ||
.recoverWith(t -> c.closeAsync().concat(failed(t))); | ||
} catch (Throwable t) { |
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.
I think we are being overly paranoid here about the calls to c.request()
or c.connect()
throwing. Any method returning an asynchronous source is not expected to throw. Having said that it is not a big deal so its ok as it is, I will leave it to you to take a call on this.
Motivation:
In case of any error or cancellation,
ProxyConnectConnectionFactoryFilter
does not close the connection to the proxy.
Modifications:
ProxyConnectConnectionFactoryFilter
;CONNECT
request or SSL handshake failures;connection will be closed anyway;
Result:
ProxyConnectConnectionFactoryFilter
does not leak connections.