jason810496 commented on code in PR #59183:
URL: https://github.com/apache/airflow/pull/59183#discussion_r2610609545


##########
airflow-core/tests/unit/assets/test_manager.py:
##########
@@ -202,3 +206,38 @@ def test_create_assets_notifies_asset_listener(self, 
session):
         assert len(asset_listener.created) == 1
         assert len(asms) == 1
         assert asset_listener.created[0].uri == asset.uri == asms[0].uri
+
+    @pytest.mark.usefixtures("dag_maker", "testing_dag_bundle")
+    def test_get_or_create_apdr_race_condition(self, session):
+        asm = AssetModel(uri="test://asset1/", name="parition_asset", 
group="asset")
+        testing_dag = DagModel(dag_id="testing_dag", is_stale=False, 
bundle_name="testing")
+        session.add_all([asm, testing_dag])
+        session.commit()
+        session.flush()
+        assert session.query(AssetPartitionDagRun).count() == 0
+
+        def _get_or_create_apdr():
+            if TYPE_CHECKING:
+                assert Session
+                assert Session.session_factory
+
+            _session = Session.session_factory()
+            _session.begin()
+            print(_session.scalar(select(AssetModel)).id)
+            try:
+                return AssetManager._get_or_create_apdr(
+                    target_key="test_partition_key",
+                    target_dag=testing_dag,
+                    asset_id=asm.id,
+                    session=_session,
+                ).id
+            finally:
+                _session.commit()
+                _session.close()
+

Review Comment:
   Since we couldn't tell whether one of the session will _always_ lock the the 
other session in the current test or not.
   The execution order of current test might be
   
   1. Non-conflict (since both thread might be start at any time)
   
   | Time | Session 1                              | Session 2                  
            |
   
|------|--------------------------------------|--------------------------------------|
   | 0    | Aquire ADPR Lock |                                      |
   | 1    |    Create ADPR  |  |
   | 2    |      |  |
   | 3    |                                      | Aquire ADPR Lock             
             |
   | 4    |                           |      Get ADPR      |
   
   2. Conflict (which test the mutex behavior)
   
   | Time | Session 1                              | Session 2                  
            |
   
|------|--------------------------------------|--------------------------------------|
   | 0    | Aquire ADPR Lock |                                      |
   | 1    |      |  Try Aquire ADPR Lock (not able to grab the Lock) |
   | 2    |   Create ADPR (commited)         |        waiting ...               
    |
   | 3    |   Commit         |        waiting ...                   |
   | 4    |                           |     Aquire ADPR Lock       |
   | 5    |                           |     Get ADPR      |
   
   The second order is what we want to test as session1 really block the 
session2.
   



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