Skip to content

Fix data race while accessing connection in partitionProducer - #701

Merged
wolfstudy merged 3 commits into
apache:masterfrom
wolfstudy:xiaolong/fix-producer-cnx
Jan 12, 2022
Merged

wolfstudy merged 3 commits into
apache:masterfrom
wolfstudy:xiaolong/fix-producer-cnx

Conversation

@wolfstudy

@wolfstudy wolfstudy commented Jan 11, 2022 •

Copy link
Copy Markdown
Member

Signed-off-by: xiaolongran [email protected]

Motivation

In #700, we use a separate go rutine to handle the logic of reconnect, so here you may encounter the same data race problem as #535

Modifications

Now, the conn field is read and written atomically; avoiding race conditions.

@wolfstudy wolfstudy added this to the v0.8.0 milestone Jan 11, 2022
@wolfstudy wolfstudy self-assigned this Jan 11, 2022
@wolfstudy

Copy link
Copy Markdown
Member Author

cc @dferstay PTAL thanks

Signed-off-by: xiaolongran <[email protected]>
Signed-off-by: xiaolongran <[email protected]>
@wolfstudy
wolfstudy requested a review from zymap January 11, 2022 06:39
p.log.Warnf("Received ack for %v on sequenceId %v - expected: %v, closing connection", response.GetMessageId(),
response.GetSequenceId(), pi.sequenceID)
p.cnx.Close()
p._getConn().Close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe clean up the cached connection when the cnx is closed? otherwise _getConn() will return a closed connection.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like #703 is working on this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@freeznet Looking at the implementation of the current Close method, when triggering the logic of Close, the state of the current connection will first be set to connectionClosed. When GetConnection() is executed, if the state of the connection is detected to be closed, the connection will be removed from the cache.

func (c *connection) Close() {
	c.closeOnce.Do(func() {
		c.Lock()
		cnx := c.cnx
		c.Unlock()
		c.changeState(connectionClosed)
if conn.closed() {
			delete(p.connections, key)
			p.log.Infof("Removed connection from pool key=%s logical_addr=%+v physical_addr=%+v",
				key, conn.logicalAddr, conn.physicalAddr)
			conn = nil // set to nil so we create a new one
		}

@leizhiyuan leizhiyuan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@wolfstudy
wolfstudy merged commit 2bcf7c7 into apache:master Jan 12, 2022
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.

4 participants