-
Notifications
You must be signed in to change notification settings - Fork 435
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
Non-atomic increment of volatile fields #191
Comments
Thanks @marschall for your inputs. |
Thank you @marschall for reporting the issue. After investigation, we totally agree with your suggestions. However, we are working on a new multi-threading feature right now, We decide to mark this issue as enhancement and re-consider it in the future after the multi-threading feature is done. Thank you. |
Yes @v-xiangs I agree it doesn't seem to be a correctness issue, just a bit confusing. |
@marschall Thank you very much for the findings! We have created a PR #409 for this issue. Thanks! |
Hello @marschall , thank you very much for your contribution. The PR is merged and we are closing this issue now. |
There are two non-atomic increments of volatile fields in
com.microsoft.sqlserver.jdbc.SocketFinder#noOfThreadsThatNotified
andcom.microsoft.sqlserver.jdbc.TDSWriter#packetNum
. In Java the pre- and post-increment operators on volatile fields are not atomic. In Java volatile fields indicate that state is intended to be changed by multiple threads.Since
com.microsoft.sqlserver.jdbc.SocketFinder#noOfThreadsThatNotified
is only used inupdateResult
and guarded bysocketFinderlock
I would removevolatile
there.com.microsoft.sqlserver.jdbc.TDSWriter#packetNum
is a bit less obvious. The warning could simply be fixed by using eitherjava.util.concurrent.atomic.AtomicInteger
orjava.util.concurrent.atomic.AtomicIntegerFieldUpdater
, the latter has a bit lower overhead. On the other hand without knowing any details about the threading model it's hard to know if that is enough or even necessary. All the other fields changed in methods wherepacketNum
is accessed seem to be non-volatile and the changes don't seem to be atomic. This suggests to me that those methods are not intended to be thread-safe and as a result removingvolatile
frompacketNum
would be my recommendation here as well.The text was updated successfully, but these errors were encountered: