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


##########
sdks/python/apache_beam/options/pipeline_construction_options.py:
##########
@@ -0,0 +1,37 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Singleton providing access to pipeline options during graph construction.
+
+This module provides a lightweight singleton that holds the full
+PipelineOptions set during Pipeline.__init__.
+"""
+
+from typing import Optional
+
+from apache_beam.options.pipeline_options import PipelineOptions
+
+
+class PipelineConstructionOptions:
+  """Holds the current pipeline's options during graph construction.
+
+  Set during Pipeline.__init__.
+  """
+  options: Optional[PipelineOptions] = None
+
+
+pipeline_construction_options = PipelineConstructionOptions()

Review Comment:
   ![critical](https://www.gstatic.com/codereviewagent/critical.svg)
   
   The current implementation of `PipelineConstructionOptions` as a singleton 
is not thread-safe. If multiple pipelines are constructed concurrently in 
different threads, they will race to write and read attributes on the single 
`pipeline_construction_options` instance. This can lead to a pipeline being 
constructed with another pipeline's options, causing unpredictable behavior.
   
   To make this thread-safe, the options should be stored in thread-local 
storage. By subclassing `threading.local`, each thread gets its own storage for 
the options, preventing race conditions.
   
   I've also explicitly declared `force_dill_deterministic_coders` for better 
static analysis, as it's being set dynamically in tests.
   
   ```python
   #
   # Licensed to the Apache Software Foundation (ASF) under one or more
   # contributor license agreements.  See the NOTICE file distributed with
   # this work for additional information regarding copyright ownership.
   # The ASF licenses this file to You under the Apache License, Version 2.0
   # (the "License"); you may not use this file except in compliance with
   # the License.  You may obtain a copy of the License at
   #
   #    http://www.apache.org/licenses/LICENSE-2.0
   #
   # Unless required by applicable law or agreed to in writing, software
   # distributed under the License is distributed on an "AS IS" BASIS,
   # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   # See the License for the specific language governing permissions and
   # limitations under the License.
   #
   
   """Singleton providing access to pipeline options during graph construction.
   
   This module provides a lightweight singleton that holds the full
   PipelineOptions set during Pipeline.__init__.
   """
   
   import threading
   from typing import Optional
   
   from apache_beam.options.pipeline_options import PipelineOptions
   
   
   class PipelineConstructionOptions(threading.local):
     """Holds the current pipeline's options during graph construction.
   
     Set during Pipeline.__init__. This is thread-local to support concurrent
     pipeline constructions.
     """
     options: Optional[PipelineOptions]
     force_dill_deterministic_coders: bool
   
   
   pipeline_construction_options = PipelineConstructionOptions()
   
   ```



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