This is an automated email from the ASF dual-hosted git repository.
zike pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git
The following commit(s) were added to refs/heads/master by this push:
new 96bc58f [fix] Fix data race in checkAndCleanIdleConnections (#981)
96bc58f is described below
commit 96bc58fd5e7ffb041703a082f81052a188f9c8ba
Author: Zike Yang <[email protected]>
AuthorDate: Tue Mar 7 17:15:00 2023 +0800
[fix] Fix data race in checkAndCleanIdleConnections (#981)
### Motivation
From the data race log of the issue:
```
2023-03-07T02:05:50.0096902Z WARNING: DATA RACE
2023-03-07T02:05:50.0097176Z Read at 0x00c0003bb298 by goroutine 111:
2023-03-07T02:05:50.0097767Z
github.com/apache/pulsar-client-go/pulsar/internal.(*connectionPool).checkAndCleanIdleConnections()
2023-03-07T02:05:50.0098468Z
/pulsar/pulsar-client-go/pulsar/internal/connection_pool.go:157 +0x24d
2023-03-07T02:05:50.0099058Z
github.com/apache/pulsar-client-go/pulsar/internal.StartCleanConnectionsTask.func1()
2023-03-07T02:05:50.0099727Z
/pulsar/pulsar-client-go/pulsar/internal/helper.go:25 +0x47
2023-03-07T02:05:50.0099934Z
2023-03-07T02:05:50.0100066Z Previous write at 0x00c0003bb298 by goroutine
69:
2023-03-07T02:05:50.0100537Z
github.com/apache/pulsar-client-go/pulsar/internal.(*connection).connect()
2023-03-07T02:05:50.0101126Z
/pulsar/pulsar-client-go/pulsar/internal/connection.go:277 +0x5d2
2023-03-07T02:05:50.0101649Z
github.com/apache/pulsar-client-go/pulsar/internal.(*connection).start.func1()
2023-03-07T02:05:50.0102222Z
/pulsar/pulsar-client-go/pulsar/internal/connection.go:230 +0x34
```
We can see that the read access of c.log here:
https://github.com/apache/pulsar-client-go/blob/c5956779f078176b6f915892dd47d9cbdb0d0fea/pulsar/internal/connection_pool.go#L157
is conflict with the write access of c.log here:
https://github.com/apache/pulsar-client-go/blob/c5956779f078176b6f915892dd47d9cbdb0d0fea/pulsar/internal/connection.go#L277
This caused the data race.
### Modifications
* Using p.log to avoid accessing the c.log
---
pulsar/internal/connection_pool.go | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/pulsar/internal/connection_pool.go
b/pulsar/internal/connection_pool.go
index bf1e297..6ff7991 100644
--- a/pulsar/internal/connection_pool.go
+++ b/pulsar/internal/connection_pool.go
@@ -154,7 +154,8 @@ func (p *connectionPool)
checkAndCleanIdleConnections(maxIdleTime time.Duration)
p.Lock()
for k, c := range p.connections {
if c.CheckIdle(maxIdleTime) {
- c.log.Debugf("Closed connection due to
inactivity.")
+ p.log.Debugf("Closed connection from
pool due to inactivity. logical_addr=%+v physical_addr=%+v",
+ c.logicalAddr, c.physicalAddr)
delete(p.connections, k)
c.Close()
}