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

Non-atomic increment of volatile fields #191

Closed
marschall opened this issue Mar 10, 2017 · 5 comments
Closed

Non-atomic increment of volatile fields #191

marschall opened this issue Mar 10, 2017 · 5 comments

Comments

@marschall
Copy link
Contributor

marschall commented Mar 10, 2017

There are two non-atomic increments of volatile fields in com.microsoft.sqlserver.jdbc.SocketFinder#noOfThreadsThatNotified and com.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 in updateResult and guarded by socketFinderlock I would remove volatile there.

com.microsoft.sqlserver.jdbc.TDSWriter#packetNum is a bit less obvious. The warning could simply be fixed by using either java.util.concurrent.atomic.AtomicInteger or java.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 where packetNum 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 removing volatile from packetNum would be my recommendation here as well.

@v-nisidh v-nisidh added this to the 6.1.7 milestone Mar 13, 2017
@v-nisidh
Copy link
Contributor

Thanks @marschall for your inputs.

@v-nisidh v-nisidh added the Under Review Used for pull requests under review label Mar 13, 2017
@xiangyushawn xiangyushawn self-assigned this Mar 23, 2017
@xiangyushawn
Copy link
Contributor

xiangyushawn commented Mar 24, 2017

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.

@xiangyushawn xiangyushawn added Enhancement An enhancement to the driver. Lower priority than bugs. and removed Under Review Used for pull requests under review labels Mar 24, 2017
@xiangyushawn xiangyushawn modified the milestones: Long Term Goals, 6.1.7 Mar 24, 2017
@xiangyushawn xiangyushawn removed their assignment Mar 24, 2017
@marschall
Copy link
Contributor Author

Yes @v-xiangs I agree it doesn't seem to be a correctness issue, just a bit confusing.

@xiangyushawn
Copy link
Contributor

xiangyushawn commented Jul 25, 2017

@marschall Thank you very much for the findings! We have created a PR #409 for this issue. Thanks!

@xiangyushawn xiangyushawn added PR Under Review and removed Enhancement An enhancement to the driver. Lower priority than bugs. labels Jul 31, 2017
@xiangyushawn
Copy link
Contributor

Hello @marschall , thank you very much for your contribution. The PR is merged and we are closing this issue now.

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

No branches or pull requests

4 participants