You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by GitBox <gi...@apache.org> on 2022/01/08 09:06:31 UTC
[GitHub] [thrift] fishy opened a new pull request #2500: go: Make socketConn.Close thread-safe
fishy opened a new pull request #2500:
URL: https://github.com/apache/thrift/pull/2500
Client: go
We used to rely on setting the connection inside TSocket/TSSLSocket as
nil after Close is called to mark the connection as closed, but that is
not thread safe and causing TSocket.Close/TSSLSocket.Close cannot be
called concurrently. Use an atomic int to mark closure instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [thrift] buptubuntu commented on a change in pull request #2500: go: Make socketConn.Close thread-safe
Posted by GitBox <gi...@apache.org>.
buptubuntu commented on a change in pull request #2500:
URL: https://github.com/apache/thrift/pull/2500#discussion_r780651460
##########
File path: lib/go/thrift/socket_conn.go
##########
@@ -100,3 +102,12 @@ func (sc *socketConn) Read(p []byte) (n int, err error) {
return sc.Conn.Read(p)
}
+
+func (sc *socketConn) Close() error {
+ if !sc.isValid() {
+ // Already closed
+ return net.ErrClosed
+ }
+ atomic.StoreInt32(&sc.closed, 1)
Review comment:
Maybe atomic.CompareAndSwapInt32 is better
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [thrift] buptubuntu commented on a change in pull request #2500: go: Make socketConn.Close thread-safe
Posted by GitBox <gi...@apache.org>.
buptubuntu commented on a change in pull request #2500:
URL: https://github.com/apache/thrift/pull/2500#discussion_r780651460
##########
File path: lib/go/thrift/socket_conn.go
##########
@@ -100,3 +102,12 @@ func (sc *socketConn) Read(p []byte) (n int, err error) {
return sc.Conn.Read(p)
}
+
+func (sc *socketConn) Close() error {
+ if !sc.isValid() {
+ // Already closed
+ return net.ErrClosed
+ }
+ atomic.StoreInt32(&sc.closed, 1)
Review comment:
Maybe atomic.CompareAndSwapInt32 is better
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [thrift] fishy commented on pull request #2500: go: Make socketConn.Close thread-safe
Posted by GitBox <gi...@apache.org>.
fishy commented on pull request #2500:
URL: https://github.com/apache/thrift/pull/2500#issuecomment-1008086248
> Quick question regarding 0.16.0: Wait for this to complete?
@Jens-G yes please.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [thrift] fishy merged pull request #2500: go: Make socketConn.Close thread-safe
Posted by GitBox <gi...@apache.org>.
fishy merged pull request #2500:
URL: https://github.com/apache/thrift/pull/2500
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [thrift] fishy commented on pull request #2500: go: Make socketConn.Close thread-safe
Posted by GitBox <gi...@apache.org>.
fishy commented on pull request #2500:
URL: https://github.com/apache/thrift/pull/2500#issuecomment-1008092740
> only atomic.CompareAndSwapInt32 is atomic, first load and later store is not
while technically that's correct, that doesn't matter here.
the problem we are solving is that inside `TSocket.Close` and `TSSLSocket.Close`, we call underlying socket's close, and set the underlying socket to nil. The race condition is about two goroutines setting the socket to nil concurrently, not about the socket's Close being called concurrently, as that's already supported by the stdlib.
so the fix here is that we use atomic int being set to non-zero to replace the old socket is nil check, and eliminate the race condition.
so we can still get into the situation that two goroutines call `Close` concurrently, they both pass the `isValid` check, and get to the point of setting the atomic int to 1. this is still fine because two goroutines calling store is fine (the winner change it from 0 to 1 and the loser doesn't really make any changes), and then they will both call the underlying Close and that's still fine (the winner will close the socket and return nil error, the loser will get an already closed error).
we can change it to compare and swap here, and return the already closed error here directly (like in the failed `isValid` check case), but the already closed error returned by the underlying Close function contains extra info about the connection itself, which is slightly better than the "naked" error we are returning here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [thrift] Jens-G commented on pull request #2500: go: Make socketConn.Close thread-safe
Posted by GitBox <gi...@apache.org>.
Jens-G commented on pull request #2500:
URL: https://github.com/apache/thrift/pull/2500#issuecomment-1007945184
Quick question regarding 0.16.0: Wait for this to complete?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@thrift.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org