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

Potential data race in ByteBufferPool #5775

Closed
gregw opened this issue Dec 8, 2020 · 4 comments · Fixed by #5778
Closed

Potential data race in ByteBufferPool #5775

gregw opened this issue Dec 8, 2020 · 4 comments · Fixed by #5778
Assignees
Labels
Sponsored This issue affects a user with a commercial support agreement

Comments

@gregw
Copy link
Contributor

gregw commented Dec 8, 2020

Jetty version
9.4.x

Description

A data race detector was run over jetty 9.4.34 and flagged a potential race at
ByteBufferPool.java:199 in org.eclipse.jetty.io.ByteBufferPool$Bucket.release(Ljava/nio/ByteBuffer;)V

@gregw gregw added the Sponsored This issue affects a user with a commercial support agreement label Dec 8, 2020
@eamonnmcmanus
Copy link

Right, there's no synchronization around the assignment to _lastUpdate or its access in getLastUpdate(), and _lastUpdate is not volatile, so this is technically not thread-safe. I think the worst that can happen is that the wrong buffer gets chosen for recycling, so this is more about performance than correctness.

@lorban
Copy link
Contributor

lorban commented Dec 9, 2020

Yeah, some JVM implementations might modify the long in two 32-bit steps, so a concurrent thread could read a corrupt timestamp. As pointed by @eamonnmcmanus that timestamp is only used to figure out which bucket to clean up so this could have some perf side-effects when that race condition occurs but that should be about it.

Nevertheless, that field should be marked volatile.

@lorban
Copy link
Contributor

lorban commented Dec 9, 2020

I used AtomicLong to pay a slightly lower cost than the volatile field accesses by using lazySet() / get() on JDK 8 and setOpaque() / getOpaque() on JDK 11.

@lorban lorban closed this as completed Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Sponsored This issue affects a user with a commercial support agreement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants