Skip to content

Fix serversocket addr race condition#928

Open
AtharvaChiplunkar12 wants to merge 6 commits intouber:devfrom
AtharvaChiplunkar12:fix-serversocket-addr-race-condition
Open

Fix serversocket addr race condition#928
AtharvaChiplunkar12 wants to merge 6 commits intouber:devfrom
AtharvaChiplunkar12:fix-serversocket-addr-race-condition

Conversation

@AtharvaChiplunkar12
Copy link

@AtharvaChiplunkar12 AtharvaChiplunkar12 commented Oct 21, 2025

Problem
The Addr() method in server_socket.go was accessing the p.listener field without mutex protection, which could cause data races when multiple goroutines access it concurrently (e.g., one goroutine calling Addr() while another calls Close(), Open(), or Listen()).

Solution
Added RLock()/RUnlock() protection around listener access for thread-safety
Changed direct nil check to use IsListening() method for consistency with the codebase
Follows the same locking pattern used in other methods (Accept(), Close(), Listen(), Open())

Sync with upstream fix apache/thrift@2620a12

Also, this PR includes all the necessary upstream fixes to ensure the file is up-to-date https://github.com/apache/thrift/blob/master/lib/go/thrift/server_socket.go

@CLAassistant
Copy link

CLAassistant commented Oct 21, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

@pulkit4tech pulkit4tech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM!

Copy link

@oneporter oneporter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this was already fixed, can we pull the fixed version? https://github.com/apache/thrift/pull/3220/files

Comment on lines +78 to +80
p.mu.Lock()
listener := p.listener
p.mu.Unlock()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why take the write lock? Can we move line 79 to 71 and only take the read lock once?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good point it will take a read lock. Since these changes were directly pulled from upstream, I assume this was overlooked there

func (p *TServerSocket) Accept() (TTransport, error) {
p.mu.RLock()
interrupted := p.interrupted
listener := p.listener
Copy link

@pnndesh pnndesh Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In https://github.com/apache/thrift/blob/master/lib/go/thrift/server_socket.go, the listener is accessed under exclusive lock. Let's do the same here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was a bug in upstream. fixed it on upstream apache/thrift@e7ab34e

@@ -26,12 +26,12 @@ import (
)

type TServerSocket struct {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add more tests in server_socket_test.go and run them with race-detection flag

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 this pull request may close these issues.

5 participants