mutianf commented on code in PR #28122:
URL: https://github.com/apache/beam/pull/28122#discussion_r1303444386


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableServiceFactory.java:
##########
@@ -75,15 +76,15 @@ static BigtableServiceEntry create(ConfigId configId, 
BigtableService service) {
 
     @Override
     public void close() {
-      int refCount =
-          refCounts.getOrDefault(getConfigId().id(), new 
AtomicInteger(0)).decrementAndGet();
-      if (refCount < 0) {
-        LOG.error(
-            "close() Ref count is < 0, configId=" + getConfigId().id() + " 
refCount=" + refCount);
-      }
-      LOG.debug("close() for config id " + getConfigId().id() + ", ref count 
is " + refCount);
-      if (refCount == 0) {
-        synchronized (lock) {
+      synchronized (lock) {
+        int refCount =
+            refCounts.getOrDefault(getConfigId().id(), new 
AtomicInteger(0)).decrementAndGet();
+        if (refCount < 0) {
+          LOG.error(
+              "close() Ref count is < 0, configId=" + getConfigId().id() + " 
refCount=" + refCount);
+        }
+        LOG.debug("close() for config id " + getConfigId().id() + ", ref count 
is " + refCount);
+        if (refCount == 0) {

Review Comment:
   I think the underlying issue is the race between:
    - close() decrementing the counter and
    - getServiceForReading() incrementing the counter
   Since decrementing the counter is outside of the lock, and we're getting the 
refCount number and do something with it, there could be a race. 
   
   When `<0`, that means refCounts doesn't has a reference to configId, so 
we're not triggering the remove there. 



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to