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

Fix reentrant subscribe in StreamingNettyByteBody #11051

Merged
merged 2 commits into from
Sep 9, 2024
Merged

Conversation

yawkat
Copy link
Member

@yawkat yawkat commented Aug 6, 2024

If a subscriber to a split streaming body writes a response when reading data from the body, this can ultimately lead to another split of the same body being closed. Closing the body in turn is a subscribe operation, which must not happen during a read in a reentrant fashion. This would lead to a failure in the assertion that guards against such reentrant operations (assert !working;), and various downstream issues like buffer leaks. In particular, MaxRequestSizeSpec was affected by this bug occasionally.

This patch replaces the use of EventLoopFlow with more suitable code. In particular, EventLoopFlow does not support reentrant or concurrent calls, it only ensures serialization. The new logic supports reentrant or concurrent calls and still ensures serialization where it matters.

The new test does not work yet due to netty/netty#13730 . This PR is a draft until that patch is released.

If a subscriber to a split streaming body writes a response when reading data from the body, this can ultimately lead to another split of the same body being closed. Closing the body in turn is a subscribe operation, which must not happen during a read in a reentrant fashion. This would lead to a failure in the assertion that guards against such reentrant operations (`assert !working;`), and various downstream issues like buffer leaks. In particular, `MaxRequestSizeSpec` was affected by this bug occasionally.

This patch replaces the use of EventLoopFlow with more suitable code. In particular, EventLoopFlow does not support reentrant or concurrent calls, it only ensures serialization. The new logic supports reentrant or concurrent calls and still ensures serialization where it matters.

The new test does not work yet due to netty/netty#13730 . This PR is a draft until that patch is released.
@yawkat yawkat added the type: bug Something isn't working label Aug 6, 2024
@graemerocher
Copy link
Contributor

@yawkat now that the netty upgrade is done can this proceed?

@yawkat
Copy link
Member Author

yawkat commented Sep 9, 2024

Sure I'll update

Copy link

sonarqubecloud bot commented Sep 9, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
64.3% Coverage on New Code (required ≥ 70%)

See analysis details on SonarCloud

@yawkat yawkat marked this pull request as ready for review September 9, 2024 12:46
@graemerocher graemerocher merged commit 7b60047 into 4.6.x Sep 9, 2024
20 of 21 checks passed
@graemerocher graemerocher deleted the reentrant-close branch September 9, 2024 12:54
@shawnone
Copy link

@yawkat Hi, it this issue merged to 4.7? I am using 4.7 and reproduce this issue

@yawkat
Copy link
Member Author

yawkat commented Nov 19, 2024

This is fixed in 4.7, yes. You are likely seeing a different issue.

@mwz1994
Copy link

mwz1994 commented Nov 19, 2024

This is fixed in 4.7, yes. You are likely seeing a different issue.

Maybe it's a similar exception.
4.7.5 throws the following stack message.But 4.6.x works fine

ExceptionMessage:
Cannot invoke "io.micronaut.http.netty.EventLoopFlow.executeNow(java.lang.Runnable)" because "this.flow" is null
ExceptionStackTrace:
java.lang.NullPointerException: Cannot invoke "io.micronaut.http.netty.EventLoopFlow.executeNow(java.lang.Runnable)" because "this.flow" is null
at io.micronaut.http.client.netty.StreamWriter.add(StreamWriter.java:90)
at io.micronaut.http.netty.body.StreamingNettyByteBody$SharedBuffer.subscribe0(StreamingNettyByteBody.java:348)
at io.micronaut.http.netty.body.StreamingNettyByteBody$SharedBuffer.subscribe(StreamingNettyByteBody.java:326)
at io.micronaut.http.netty.body.StreamingNettyByteBody.primary(StreamingNettyByteBody.java:92)
at io.micronaut.http.client.netty.StreamWriter.(StreamWriter.java:53)
at io.micronaut.http.client.netty.DefaultHttpClient.lambda$sendRawRequest$48(DefaultHttpClient.java:1667)
at reactor.core.publisher.MonoCreate.subscribe(MonoCreate.java:61)
at reactor.core.publisher.Mono.subscribe(Mono.java:4576)
at reactor.core.publisher.MonoSubscribeOn$SubscribeOnSubscriber.run(MonoSubscribeOn.java:126)
at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84)
at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37)
at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:96)
at io.netty.util.concurrent.PromiseTask.run(PromiseTask.java:106)
at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:173)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:166)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:1583)

@yawkat
Copy link
Member Author

yawkat commented Nov 19, 2024

looks like a bug, please make a new issue.

@mwz1994
Copy link

mwz1994 commented Nov 21, 2024

looks like a bug, please make a new issue.

Thanks for the reply, I created a new issue. (#11364

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants