Thanks to visit codestin.com
Credit goes to github.com

Skip to content

NPE in at org.asynchttpclient.netty.channel.Channels.setAttribute(Channels.java:36) #2042

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

Closed
xiaoawo opened this issue Dec 4, 2024 · 6 comments · Fixed by #2088
Closed

Comments

@xiaoawo
Copy link

xiaoawo commented Dec 4, 2024

java.lang.NullPointerException
at org.asynchttpclient.netty.channel.Channels.setAttribute(Channels.java:36)
at org.asynchttpclient.netty.channel.Channels.setDiscard(Channels.java:40)
at org.asynchttpclient.netty.NettyResponseFuture.cancel(NettyResponseFuture.java:183)

@xiaoawo
Copy link
Author

xiaoawo commented Dec 4, 2024

I noticed that in the done() and abort() methods, the channel is set to null. If the code at line 182 (channel != null) is executed at this time, and another thread executes done() or abort(), it may lead to a NullPointerException at org.asynchttpclient.netty.channel.Channels.setAttribute(Channels.java:36).

@hyperxpro
Copy link
Member

This is likely a race condition. Do you plan for a PR?

@xiaoawo
Copy link
Author

xiaoawo commented Dec 6, 2024

This is likely a race condition. Do you plan for a PR?

yes,I will handle it in the next two days and provide a PR.

@pratt4
Copy link
Contributor

pratt4 commented May 5, 2025

Hi, I’d like to work on this
if it's still not done

@hyperxpro
Copy link
Member

Hi, I’d like to work on this
if it's still not done

Sure, please send a PR.

@pratt4
Copy link
Contributor

pratt4 commented May 7, 2025

Hi, I have opened a PR, please review

pratt4 pushed a commit to pratt4/async-http-client that referenced this issue May 9, 2025
hyperxpro pushed a commit that referenced this issue May 9, 2025
Fixes #2042 

This is a typical TOCTOU (time-of-check/time-of-use) race
https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use.
The NPE was occurring because the channel field could be set to null by
another thread between the check and its use:

if (channel != null) {                 // time-of-check
    Channels.setDiscard(channel);      //  time-of-use
    Channels.silentlyCloseChannel(channel);
}

By copying channel into a local variable in one atomic read, we ensure
that—even if another thread changes the field—the local reference
remains valid.

P.S. It is hard to write a deterministic test that fails consistently,
so this PR only includes the code fix.

---------

Co-authored-by: prat <[email protected]>
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 a pull request may close this issue.

3 participants