gemini-code-assist[bot] commented on code in PR #38851:
URL: https://github.com/apache/beam/pull/38851#discussion_r3376227118


##########
sdks/python/apache_beam/runners/worker/statesampler_fast.pyx:
##########
@@ -217,7 +217,11 @@ cdef class ScopedState(object):
 
   @property
   def nsecs(self):
-    return self._nsecs
+    cdef int64_t val
+    pythread.PyThread_acquire_lock(self.sampler.lock, pythread.WAIT_LOCK)
+    val = self._nsecs
+    pythread.PyThread_release_lock(self.sampler.lock)
+    return val

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Acquiring a C-level lock while holding the Python GIL can block the entire 
Python interpreter if there is contention on the lock (e.g., from the 
background sampler thread). Releasing the GIL using `with nogil:` during the 
lock acquisition and value read prevents other Python threads from being 
blocked.
   
   ```
       cdef int64_t val
       with nogil:
         pythread.PyThread_acquire_lock(self.sampler.lock, pythread.WAIT_LOCK)
         val = self._nsecs
         pythread.PyThread_release_lock(self.sampler.lock)
       return val
   ```



-- 
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