All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fetch: Don't reuse the url data cached for a freed datastore
@ 2026-11-28 22:03 Ricardo Salveti
  2026-11-29 20:52 ` Paul Barker
  0 siblings, 1 reply; 3+ messages in thread
From: Ricardo Salveti @ 2026-11-28 22:03 UTC (permalink / raw)
  To: bitbake-devel

The url data cache is keyed by the recipe file and id(d), and an entry
stays after its datastore is freed. Python reuses the id of a freed
object, so a new datastore of the same recipe can get the freed one's
url data: its urls on top of the new ones, or silently its SRCREV.

meta-qcom's oe-selftest has been flaky since oe-core 4746cd7138bc
("oeqa/utils/commands: use tinfoil in get_bb_vars"): back-to-back
tinfoil sessions reuse one server, each parses a recipe in it, and the
parsers forked later inherit its cache. A recipe whose BBCLASSEXTEND
variants use different git urls then sometimes fails to parse:

  The SRCREV_FORMAT variable must be set when multiple SCMs are used.
  The SCMs are:
  git://github.com/qualcomm-linux/kernel.git;nobranch=1;protocol=https
  git://github.com/qualcomm-linux/kernel.git;branch=qcom-next;protocol=https

Keep a weak reference to the datastore with each entry and only use the
entry for that datastore. Dead entries stay in the cache, as before.

A new test gives two datastores the same id and fails without this.

Fixes: 9a5dd1be6339 ("fetch2: Fix urldata_cache key issues")
AI-Generated: Uses Claude Code (Claude Fable 5.2)
Signed-off-by: Ricardo Salveti <ricardo.salveti@oss.qualcomm.com>
---
 lib/bb/fetch/__init__.py |  9 +++++++--
 lib/bb/tests/fetch.py    | 23 +++++++++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/lib/bb/fetch/__init__.py b/lib/bb/fetch/__init__.py
index 612336170..76e0778d1 100644
--- a/lib/bb/fetch/__init__.py
+++ b/lib/bb/fetch/__init__.py
@@ -24,6 +24,7 @@ import subprocess
 import pickle
 import errno
 import shlex
+import weakref
 import bb.utils
 import bb.checksum
 import bb.process
@@ -1863,7 +1864,11 @@ class Fetch(object):
         if cache and fn:
             key = mc + fn + str(id(d))
         if key in urldata_cache:
-            self.ud = urldata_cache[key]
+            # id() values get reused once a datastore is freed, so check the
+            # entry was stored for this datastore
+            cached_d, cached_ud = urldata_cache[key]
+            if cached_d() is d:
+                self.ud = cached_ud
 
         # the unpack_tracer object needs to be made available to possible nested
         # Fetch instances (when those are created by gitsm and npmsw fetchers)
@@ -1895,7 +1900,7 @@ class Fetch(object):
                         pass
 
         if key:
-            urldata_cache[key] = self.ud
+            urldata_cache[key] = (weakref.ref(d), self.ud)
 
     def localpath(self, url):
         if url not in self.urls:
diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
index 104ee1f68..23ec5ddc1 100644
--- a/lib/bb/tests/fetch.py
+++ b/lib/bb/tests/fetch.py
@@ -454,6 +454,29 @@ class FetcherTest(unittest.TestCase):
         except bb.process.ExecutionError:
             self.git(['config', 'user.name', 'Your Name'], cwd=cwd)
 
+class UrldataCacheTest(FetcherTest):
+    def recipe(self, branch):
+        d = self.d.createCopy()
+        d.setVar("FILE", "/recipes/foo_git.bb")
+        d.setVar("SRCREV", "82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40")
+        d.setVar("SRC_URI", "git://git.openembedded.org/bitbake;%s;protocol=https" % branch)
+        return d
+
+    def test_same_datastore(self):
+        d = self.recipe("branch=master")
+        self.assertIs(bb.fetch.Fetch([], d).ud, bb.fetch.Fetch([], d).ud)
+
+    def test_reused_datastore_id(self):
+        # The cache key uses id(d) and Python reuses the id of a freed object,
+        # so give two datastores of one recipe the same id
+        with unittest.mock.patch.object(bb.fetch, "id", create=True, return_value=0) as fake_id:
+            d1 = self.recipe("nobranch=1")
+            bb.fetch.Fetch([], d1)
+            d2 = self.recipe("branch=master")
+            fetcher = bb.fetch.Fetch([], d2)
+        self.assertTrue(fake_id.called)
+        self.assertEqual(list(fetcher.ud), d2.getVar("SRC_URI").split())
+
 class MirrorUriTest(FetcherTest):
 
     replaceuris = {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] fetch: Don't reuse the url data cached for a freed datastore
  2026-11-28 22:03 [PATCH] fetch: Don't reuse the url data cached for a freed datastore Ricardo Salveti
@ 2026-11-29 20:52 ` Paul Barker
  2026-11-29 21:22   ` Paul Barker
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Barker @ 2026-11-29 20:52 UTC (permalink / raw)
  To: Ricardo Salveti, bitbake-devel

On Sat Nov 28, 2026 at 10:03 PM GMT, Ricardo Salveti wrote:
> The url data cache is keyed by the recipe file and id(d), and an entry
> stays after its datastore is freed. Python reuses the id of a freed
> object, so a new datastore of the same recipe can get the freed one's
> url data: its urls on top of the new ones, or silently its SRCREV.
>
> meta-qcom's oe-selftest has been flaky since oe-core 4746cd7138bc
> ("oeqa/utils/commands: use tinfoil in get_bb_vars"): back-to-back
> tinfoil sessions reuse one server, each parses a recipe in it, and the
> parsers forked later inherit its cache. A recipe whose BBCLASSEXTEND
> variants use different git urls then sometimes fails to parse:
>
>   The SRCREV_FORMAT variable must be set when multiple SCMs are used.
>   The SCMs are:
>   git://github.com/qualcomm-linux/kernel.git;nobranch=1;protocol=https
>   git://github.com/qualcomm-linux/kernel.git;branch=qcom-next;protocol=https
>
> Keep a weak reference to the datastore with each entry and only use the
> entry for that datastore. Dead entries stay in the cache, as before.
>
> A new test gives two datastores the same id and fails without this.
>
> Fixes: 9a5dd1be6339 ("fetch2: Fix urldata_cache key issues")
> AI-Generated: Uses Claude Code (Claude Fable 5.2)
> Signed-off-by: Ricardo Salveti <ricardo.salveti@oss.qualcomm.com>
> ---
>  lib/bb/fetch/__init__.py |  9 +++++++--
>  lib/bb/tests/fetch.py    | 23 +++++++++++++++++++++++
>  2 files changed, 30 insertions(+), 2 deletions(-)
>
> diff --git a/lib/bb/fetch/__init__.py b/lib/bb/fetch/__init__.py
> index 612336170..76e0778d1 100644
> --- a/lib/bb/fetch/__init__.py
> +++ b/lib/bb/fetch/__init__.py
> @@ -24,6 +24,7 @@ import subprocess
>  import pickle
>  import errno
>  import shlex
> +import weakref
>  import bb.utils
>  import bb.checksum
>  import bb.process
> @@ -1863,7 +1864,11 @@ class Fetch(object):
>          if cache and fn:
>              key = mc + fn + str(id(d))
>          if key in urldata_cache:
> -            self.ud = urldata_cache[key]
> +            # id() values get reused once a datastore is freed, so check the
> +            # entry was stored for this datastore
> +            cached_d, cached_ud = urldata_cache[key]
> +            if cached_d() is d:
> +                self.ud = cached_ud
>  
>          # the unpack_tracer object needs to be made available to possible nested
>          # Fetch instances (when those are created by gitsm and npmsw fetchers)
> @@ -1895,7 +1900,7 @@ class Fetch(object):
>                          pass
>  
>          if key:
> -            urldata_cache[key] = self.ud
> +            urldata_cache[key] = (weakref.ref(d), self.ud)

Hi Ricardo,

This and the above is basically the same fix I have locally after some
experimentation with Claude to try to fix bugs #15848 & #16382. This
alone isn't a complete fix for both of those, but it looks right and
it's one step closer to the fix. I can follow up with other patches.

>  
>      def localpath(self, url):
>          if url not in self.urls:
> diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
> index 104ee1f68..23ec5ddc1 100644
> --- a/lib/bb/tests/fetch.py
> +++ b/lib/bb/tests/fetch.py
> @@ -454,6 +454,29 @@ class FetcherTest(unittest.TestCase):
>          except bb.process.ExecutionError:
>              self.git(['config', 'user.name', 'Your Name'], cwd=cwd)
>  
> +class UrldataCacheTest(FetcherTest):
> +    def recipe(self, branch):
> +        d = self.d.createCopy()
> +        d.setVar("FILE", "/recipes/foo_git.bb")
> +        d.setVar("SRCREV", "82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40")
> +        d.setVar("SRC_URI", "git://git.openembedded.org/bitbake;%s;protocol=https" % branch)
> +        return d
> +
> +    def test_same_datastore(self):
> +        d = self.recipe("branch=master")
> +        self.assertIs(bb.fetch.Fetch([], d).ud, bb.fetch.Fetch([], d).ud)
> +
> +    def test_reused_datastore_id(self):
> +        # The cache key uses id(d) and Python reuses the id of a freed object,
> +        # so give two datastores of one recipe the same id
> +        with unittest.mock.patch.object(bb.fetch, "id", create=True, return_value=0) as fake_id:
> +            d1 = self.recipe("nobranch=1")
> +            bb.fetch.Fetch([], d1)
> +            d2 = self.recipe("branch=master")
> +            fetcher = bb.fetch.Fetch([], d2)
> +        self.assertTrue(fake_id.called)
> +        self.assertEqual(list(fetcher.ud), d2.getVar("SRC_URI").split())

This is better than the test case I have which messes around triggering
garbage collection, nice!

Best regards,

-- 
Paul Barker


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] fetch: Don't reuse the url data cached for a freed datastore
  2026-11-29 20:52 ` Paul Barker
@ 2026-11-29 21:22   ` Paul Barker
  0 siblings, 0 replies; 3+ messages in thread
From: Paul Barker @ 2026-11-29 21:22 UTC (permalink / raw)
  To: Ricardo Salveti, bitbake-devel

On Sun Nov 29, 2026 at 8:52 PM GMT, Paul Barker wrote:
> On Sat Nov 28, 2026 at 10:03 PM GMT, Ricardo Salveti wrote:
>> The url data cache is keyed by the recipe file and id(d), and an entry
>> stays after its datastore is freed. Python reuses the id of a freed
>> object, so a new datastore of the same recipe can get the freed one's
>> url data: its urls on top of the new ones, or silently its SRCREV.
>>
>> meta-qcom's oe-selftest has been flaky since oe-core 4746cd7138bc
>> ("oeqa/utils/commands: use tinfoil in get_bb_vars"): back-to-back
>> tinfoil sessions reuse one server, each parses a recipe in it, and the
>> parsers forked later inherit its cache. A recipe whose BBCLASSEXTEND
>> variants use different git urls then sometimes fails to parse:
>>
>>   The SRCREV_FORMAT variable must be set when multiple SCMs are used.
>>   The SCMs are:
>>   git://github.com/qualcomm-linux/kernel.git;nobranch=1;protocol=https
>>   git://github.com/qualcomm-linux/kernel.git;branch=qcom-next;protocol=https
>>
>> Keep a weak reference to the datastore with each entry and only use the
>> entry for that datastore. Dead entries stay in the cache, as before.
>>
>> A new test gives two datastores the same id and fails without this.
>>
>> Fixes: 9a5dd1be6339 ("fetch2: Fix urldata_cache key issues")
>> AI-Generated: Uses Claude Code (Claude Fable 5.2)
>> Signed-off-by: Ricardo Salveti <ricardo.salveti@oss.qualcomm.com>
>> ---
>>  lib/bb/fetch/__init__.py |  9 +++++++--
>>  lib/bb/tests/fetch.py    | 23 +++++++++++++++++++++++
>>  2 files changed, 30 insertions(+), 2 deletions(-)
>>
>> diff --git a/lib/bb/fetch/__init__.py b/lib/bb/fetch/__init__.py
>> index 612336170..76e0778d1 100644
>> --- a/lib/bb/fetch/__init__.py
>> +++ b/lib/bb/fetch/__init__.py
>> @@ -24,6 +24,7 @@ import subprocess
>>  import pickle
>>  import errno
>>  import shlex
>> +import weakref
>>  import bb.utils
>>  import bb.checksum
>>  import bb.process
>> @@ -1863,7 +1864,11 @@ class Fetch(object):
>>          if cache and fn:
>>              key = mc + fn + str(id(d))
>>          if key in urldata_cache:
>> -            self.ud = urldata_cache[key]
>> +            # id() values get reused once a datastore is freed, so check the
>> +            # entry was stored for this datastore
>> +            cached_d, cached_ud = urldata_cache[key]
>> +            if cached_d() is d:
>> +                self.ud = cached_ud
>>  
>>          # the unpack_tracer object needs to be made available to possible nested
>>          # Fetch instances (when those are created by gitsm and npmsw fetchers)
>> @@ -1895,7 +1900,7 @@ class Fetch(object):
>>                          pass
>>  
>>          if key:
>> -            urldata_cache[key] = self.ud
>> +            urldata_cache[key] = (weakref.ref(d), self.ud)
>
> Hi Ricardo,
>
> This and the above is basically the same fix I have locally after some
> experimentation with Claude to try to fix bugs #15848 & #16382. This
> alone isn't a complete fix for both of those, but it looks right and
> it's one step closer to the fix. I can follow up with other patches.

Actually, Claude was being stupid. No surprises there I supposed.

This patch alone should be enough to fix #15848 & #16382.

>
>>  
>>      def localpath(self, url):
>>          if url not in self.urls:
>> diff --git a/lib/bb/tests/fetch.py b/lib/bb/tests/fetch.py
>> index 104ee1f68..23ec5ddc1 100644
>> --- a/lib/bb/tests/fetch.py
>> +++ b/lib/bb/tests/fetch.py
>> @@ -454,6 +454,29 @@ class FetcherTest(unittest.TestCase):
>>          except bb.process.ExecutionError:
>>              self.git(['config', 'user.name', 'Your Name'], cwd=cwd)
>>  
>> +class UrldataCacheTest(FetcherTest):
>> +    def recipe(self, branch):
>> +        d = self.d.createCopy()
>> +        d.setVar("FILE", "/recipes/foo_git.bb")
>> +        d.setVar("SRCREV", "82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40")
>> +        d.setVar("SRC_URI", "git://git.openembedded.org/bitbake;%s;protocol=https" % branch)
>> +        return d
>> +
>> +    def test_same_datastore(self):
>> +        d = self.recipe("branch=master")
>> +        self.assertIs(bb.fetch.Fetch([], d).ud, bb.fetch.Fetch([], d).ud)
>> +
>> +    def test_reused_datastore_id(self):
>> +        # The cache key uses id(d) and Python reuses the id of a freed object,
>> +        # so give two datastores of one recipe the same id
>> +        with unittest.mock.patch.object(bb.fetch, "id", create=True, return_value=0) as fake_id:
>> +            d1 = self.recipe("nobranch=1")
>> +            bb.fetch.Fetch([], d1)
>> +            d2 = self.recipe("branch=master")
>> +            fetcher = bb.fetch.Fetch([], d2)
>> +        self.assertTrue(fake_id.called)
>> +        self.assertEqual(list(fetcher.ud), d2.getVar("SRC_URI").split())
>
> This is better than the test case I have which messes around triggering
> garbage collection, nice!

Best regards,

-- 
Paul Barker


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-29 21:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 22:03 [PATCH] fetch: Don't reuse the url data cached for a freed datastore Ricardo Salveti
2026-11-29 20:52 ` Paul Barker
2026-11-29 21:22   ` Paul Barker

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.