<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: create_auto_cdc_from_snapshot_flow Python session resolution fails if having multiple snapshot f in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167281#M55666</link>
    <description>&lt;P&gt;Thank you! I am still doing something wrong though, my function is now called like so:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;dp.&lt;/SPAN&gt;&lt;SPAN&gt;create_auto_cdc_from_snapshot_flow&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;target&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;SILVER_TABLE,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;source&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;lambda&lt;/SPAN&gt; &lt;SPAN&gt;latest_version&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;SPAN&gt;next_locations_snapshot_and_version&lt;/SPAN&gt;&lt;SPAN&gt;(spark, latest_version),&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;keys&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"location_id"&lt;/SPAN&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;stored_as_scd_type&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;track_history_except_column_list&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"file_modification_time"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"source_file"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"ingestion_time"&lt;/SPAN&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;anything more i need to do to get to "pass the thread-safe `spark` instance"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Wed, 02 Sep 2026 10:50:48 GMT</pubDate>
    <dc:creator>david_aspegren</dc:creator>
    <dc:date>2026-09-02T10:50:48Z</dc:date>
    <item>
      <title>create_auto_cdc_from_snapshot_flow Python session resolution fails if having multiple snapshot flows</title>
      <link>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167268#M55663</link>
      <description>&lt;P&gt;When a pipeline contains more than one create_auto_cdc_from_snapshot_flow flow (each driven by a custom Python next_snapshot_and_version function), flow resolution fails intermittently/consistently with:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;RuntimeError: The original Spark session is being accessed instead of the per-flow cloned session during parallel analysis. This is commonly caused by spawning threads inside a flow function that access the Spark session.&lt;/P&gt;&lt;P&gt;I am having functions to figure out the next snapshot like this:&lt;BR /&gt;```&lt;BR /&gt;def next_x_snapshot_and_version(latest_version):&lt;BR /&gt;versions = spark.read.table(SOURCE_TABLE).select("file_modification_time").distinct()&lt;BR /&gt;```&lt;BR /&gt;&lt;BR /&gt;Is this a bug?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Sep 2026 09:43:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167268#M55663</guid>
      <dc:creator>david_aspegren</dc:creator>
      <dc:date>2026-09-02T09:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: create_auto_cdc_from_snapshot_flow Python session resolution fails if having multiple snapshot f</title>
      <link>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167276#M55665</link>
      <description>&lt;P&gt;This error occurs because your custom `next_x_snapshot_and_version` function references the global `spark` session variable directly, bypassing Delta Live Tables' per-flow cloned session during parallel execution. When running multiple CDC snapshot flows concurrently, DLT isolates each flow using its own cloned session; accessing global `spark` state breaks this thread safety. To fix it, update your function signature to accept a `spark_session` argument explicitly (e.g., `def next_x_snapshot_and_version(spark_session, latest_version) and pass the thread-safe `spark` instance into your custom function via a `lambda` inside your `create_auto_cdc_from_snapshot_flow` call.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Sep 2026 10:12:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167276#M55665</guid>
      <dc:creator>ThomasBehne</dc:creator>
      <dc:date>2026-09-02T10:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: create_auto_cdc_from_snapshot_flow Python session resolution fails if having multiple snapshot f</title>
      <link>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167281#M55666</link>
      <description>&lt;P&gt;Thank you! I am still doing something wrong though, my function is now called like so:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;dp.&lt;/SPAN&gt;&lt;SPAN&gt;create_auto_cdc_from_snapshot_flow&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;target&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;SILVER_TABLE,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;source&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;lambda&lt;/SPAN&gt; &lt;SPAN&gt;latest_version&lt;/SPAN&gt;&lt;SPAN&gt;: &lt;/SPAN&gt;&lt;SPAN&gt;next_locations_snapshot_and_version&lt;/SPAN&gt;&lt;SPAN&gt;(spark, latest_version),&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;keys&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"location_id"&lt;/SPAN&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;stored_as_scd_type&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;track_history_except_column_list&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"file_modification_time"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"source_file"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"ingestion_time"&lt;/SPAN&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;anything more i need to do to get to "pass the thread-safe `spark` instance"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 02 Sep 2026 10:50:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167281#M55666</guid>
      <dc:creator>david_aspegren</dc:creator>
      <dc:date>2026-09-02T10:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: create_auto_cdc_from_snapshot_flow Python session resolution fails if having multiple snapshot f</title>
      <link>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167285#M55667</link>
      <description>&lt;P&gt;Try this&lt;BR /&gt;In your next_x function&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def next_x_snapshot_and_version(latest_version):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;versions = spark.read.table(SOURCE_TABLE).select("file_modification_time").distinct()&lt;BR /&gt;Replace with&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;def next_x_snapshot_and_version(latest_version):&lt;BR /&gt;&lt;STRONG&gt;active_spark = SparkSession.getActiveSession()&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;versions = &lt;STRONG&gt;active_spark&lt;/STRONG&gt; .read.table(SOURCE_TABLE).select("file_modification_time").distinct()&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;In this Below code, Remvoe Blod code,&amp;nbsp;&lt;STRONG&gt;lambda&amp;nbsp;latest_version&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt;:, directly call the function&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;dp.&lt;/SPAN&gt;&lt;SPAN&gt;create_auto_cdc_from_snapshot_flow&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;target&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;SILVER_TABLE,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;source&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;STRONG&gt;lambda&amp;nbsp;latest_version&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt;:&lt;/STRONG&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;next_locations_snapshot_and_version&lt;/SPAN&gt;&lt;SPAN&gt;(spark, latest_version),&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;keys&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"location_id"&lt;/SPAN&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;stored_as_scd_type&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;track_history_except_column_list&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;"file_modification_time"&lt;/SPAN&gt;&lt;SPAN&gt;,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;"source_file"&lt;/SPAN&gt;&lt;SPAN&gt;,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;"ingestion_time"&lt;/SPAN&gt;&lt;SPAN&gt;],&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 02 Sep 2026 11:21:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167285#M55667</guid>
      <dc:creator>Satyasai</dc:creator>
      <dc:date>2026-09-02T11:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: create_auto_cdc_from_snapshot_flow Python session resolution fails if having multiple snapshot f</title>
      <link>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167289#M55669</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/250309"&gt;@david_aspegren&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;I don't think this is necessarily a bug. It looks more like an issue with how the custom next_snapshot_and_version() function is being evaluated when multiple create_auto_cdc_from_snapshot_flow flows are analysed in parallel.&lt;/P&gt;&lt;P&gt;In your example:&lt;/P&gt;&lt;P&gt;def next_x_snapshot_and_version(latest_version):&lt;BR /&gt;versions = (&lt;BR /&gt;spark.read.table(SOURCE_TABLE)&lt;BR /&gt;.select("file_modification_time")&lt;BR /&gt;.distinct()&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;the function is directly accessing the global spark session. During parallel flow analysis, Lakeflow uses a cloned Spark session for each flow, so reaching back to the original spark session can cause the error you're seeing.&lt;/P&gt;&lt;P&gt;I would make the next_snapshot_and_version() function a pure Python function, no spark, dbutils, table reads, or other external state inside it.&lt;/P&gt;&lt;P&gt;For example, if the next version is simply based on the current version:&lt;/P&gt;&lt;P&gt;def next_snapshot_and_version(latest_version):&lt;BR /&gt;next_version = latest_version + 1&lt;BR /&gt;next_snapshot = f"snapshot_{next_version}"&lt;/P&gt;&lt;P&gt;return next_snapshot, next_version&lt;/P&gt;&lt;P&gt;If you need to choose the next snapshot from a list, you can still keep the function pure:&lt;/P&gt;&lt;P&gt;def next_snapshot_and_version(latest_version, available_snapshots):&lt;BR /&gt;candidates = [&lt;BR /&gt;s for s in available_snapshots&lt;BR /&gt;if s["version"] &amp;gt; latest_version&lt;BR /&gt;]&lt;/P&gt;&lt;P&gt;if not candidates:&lt;BR /&gt;return None&lt;/P&gt;&lt;P&gt;next_snapshot = min(&lt;BR /&gt;candidates,&lt;BR /&gt;key=lambda x: x["version"]&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;return next_snapshot["path"], next_snapshot["version"]&lt;/P&gt;&lt;P&gt;The important part is that available_snapshots should be obtained outside the function, in the appropriate pipeline/flow context. The function itself only works with the values passed to it.&lt;/P&gt;&lt;P&gt;So instead of doing this:&lt;/P&gt;&lt;P&gt;def next_snapshot_and_version(latest_version):&lt;BR /&gt;## Spark access inside the custom function&lt;BR /&gt;df = spark.read.table(SOURCE_TABLE)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I would use this pattern:&lt;/P&gt;&lt;P&gt;##### Spark/table lookup happens in the appropriate context&lt;BR /&gt;available_snapshots = ...&lt;/P&gt;&lt;P&gt;##### Pure Python function does the calculation&lt;BR /&gt;next_snapshot, next_version = next_snapshot_and_version(&lt;BR /&gt;latest_version,&lt;BR /&gt;available_snapshots&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;I would also avoid using shared global variables or mutable state between the different CDC flows. Each flow should ideally have its own configuration and be independently resolvable.&lt;/P&gt;&lt;P&gt;So, in short, the key change I would make is:&lt;/P&gt;&lt;P&gt;Keep next_snapshot_and_version() Spark-free.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Sep 2026 11:55:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/create-auto-cdc-from-snapshot-flow-python-session-resolution/m-p/167289#M55669</guid>
      <dc:creator>srini_ve</dc:creator>
      <dc:date>2026-09-02T11:55:06Z</dc:date>
    </item>
  </channel>
</rss>

