[PATCH] scripts/spdxcheck: Limit the scope of git.Repo

Ricardo Ribalda posted 1 patch 11 months, 2 weeks ago
There is a newer version of this series
scripts/spdxcheck.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Ricardo Ribalda 11 months, 2 weeks ago
If the git.Repo object's scope extends to the Python interpreter's
shutdown phase, its destructor may fail due to the interpreter's state.

Exception ignored in: <function Git.AutoInterrupt.__del__ at 0x7f1941dd5620>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/git/cmd.py", line 565, in __del__
  File "/usr/lib/python3/dist-packages/git/cmd.py", line 546, in _terminate
  File "/usr/lib/python3.13/subprocess.py", line 2227, in terminate
ImportError: sys.meta_path is None, Python is likely shutting down

Use the `with` statement to limit the scope of git.Repo and ensure
proper resource management.

Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
 scripts/spdxcheck.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/scripts/spdxcheck.py b/scripts/spdxcheck.py
index 8d608f61bf37..eba808cbaeeb 100755
--- a/scripts/spdxcheck.py
+++ b/scripts/spdxcheck.py
@@ -349,11 +349,11 @@ if __name__ == '__main__':
 
     try:
         # Use git to get the valid license expressions
-        repo = git.Repo(os.getcwd())
-        assert not repo.bare
+        with git.Repo(os.getcwd()) as repo:
+            assert not repo.bare
 
-        # Initialize SPDX data
-        spdx = read_spdxdata(repo)
+            # Initialize SPDX data
+            spdx = read_spdxdata(repo)
 
         # Initialize the parser
         parser = id_parser(spdx)

---
base-commit: d082ecbc71e9e0bf49883ee4afd435a77a5101b6
change-id: 20250225-spx-382cf543370e

Best regards,
-- 
Ricardo Ribalda <ribalda@chromium.org>
Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Duje Mihanović 10 months ago
On Tuesday, 25 February 2025 14:10:41 Central European Summer Time Ricardo 
Ribalda wrote:
> If the git.Repo object's scope extends to the Python interpreter's
> shutdown phase, its destructor may fail due to the interpreter's state.
> 
> Exception ignored in: <function Git.AutoInterrupt.__del__ at 0x7f1941dd5620>
> Traceback (most recent call last):
>   File "/usr/lib/python3/dist-packages/git/cmd.py", line 565, in __del__
>   File "/usr/lib/python3/dist-packages/git/cmd.py", line 546, in _terminate
>   File "/usr/lib/python3.13/subprocess.py", line 2227, in terminate
> ImportError: sys.meta_path is None, Python is likely shutting down
> 
> Use the `with` statement to limit the scope of git.Repo and ensure
> proper resource management.
> 
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---

checkpatch suddenly broke for me with the same error as shown here and the 
patch fixed it.

Tested-by: Duje Mihanović <duje.mihanovic@skole.hr>

Regards,
-- 
Duje
Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Gon Solo 10 months ago
> checkpatch suddenly broke for me with the same error as shown here and the 
> patch fixed it.
> 
> Tested-by: Duje Mihanović <duje.mihanovic@skole.hr>

Same for me.

Tested-by: Andreas Wendleder <gonsolo@gmail.com>

Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Gon Solo 10 months ago
> > checkpatch suddenly broke for me with the same error as shown here and the 
> > patch fixed it.
> > 
> > Tested-by: Duje Mihanović <duje.mihanovic@skole.hr>

Turns out, it was not enough; the variable is used later.
How about the following patch?

From 763f25c8ca2e29f343bfd109a17501de71b38d43 Mon Sep 17 00:00:00 2001
From: Andreas Wendleder <gonsolo@gmail.com>
Date: Tue, 8 Apr 2025 11:21:17 +0200
Subject: [PATCH] Fix spdxcheck.py.

As explained in Ricardo Ribalda's patch:

If the git.Repo object's scope extends to the Python interpreter's
shutdown phase, its destructor may fail due to the interpreter's state.

Exception ignored in: <function Git.AutoInterrupt.__del__ at 0x76e6b0148040>
Traceback (most recent call last):
  File "/usr/lib/python3.13/site-packages/git/cmd.py", line 790, in __del__
  File "/usr/lib/python3.13/site-packages/git/cmd.py", line 781, in _terminate
  File "/usr/lib/python3.13/subprocess.py", line 2227, in terminate
ImportError: sys.meta_path is None, Python is likely shutting down

Unfortunately, repo is used later at line 392 and 399, so we have to
keep it and manually delete it before exiting. This can be checked by
testing a directory instead of a file.

Signed-off--by: Andreas Wendleder <gonsolo@gmail.com>
---
 scripts/spdxcheck.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/scripts/spdxcheck.py b/scripts/spdxcheck.py
index 8d608f61bf37..6a89e2b2faba 100755
--- a/scripts/spdxcheck.py
+++ b/scripts/spdxcheck.py
@@ -448,6 +448,7 @@ if __name__ == '__main__':
                     for f in sorted(di.files):
                         sys.stderr.write('    %s\n' %f)
 
+            del repo
             sys.exit(0)
 
     except Exception as ex:
-- 
2.49.0

Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Gon Solo 10 months ago
It's a known problem:
https://github.com/gitpython-developers/GitPython/issues/2003
https://github.com/python/cpython/issues/118761#issuecomment-2661504264
Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Ricardo Ribalda 10 months, 1 week ago
Friendly ping
Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Greg Kroah-Hartman 10 months, 1 week ago
On Thu, Apr 03, 2025 at 11:34:14PM +0200, Ricardo Ribalda wrote:
> Friendly ping

Empty pings provide no context at all :(
Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Ricardo Ribalda 10 months, 1 week ago
On Fri, 4 Apr 2025 at 08:22, Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Thu, Apr 03, 2025 at 11:34:14PM +0200, Ricardo Ribalda wrote:
> > Friendly ping
>
> Empty pings provide no context at all :(

Do you mean that I'd rather left the whole patch as context, or that I
should provide a reason for the ping?

Let me try again:

Is there any change needed for
https://lore.kernel.org/linux-spdx/2025040417-aspire-relenting-5462@gregkh/T/#t

that was sent for review over a month ago?

Regards!


-- 
Ricardo Ribalda
Re: [PATCH] scripts/spdxcheck: Limit the scope of git.Repo
Posted by Greg Kroah-Hartman 10 months, 1 week ago
On Fri, Apr 04, 2025 at 08:29:21AM +0200, Ricardo Ribalda wrote:
> On Fri, 4 Apr 2025 at 08:22, Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Thu, Apr 03, 2025 at 11:34:14PM +0200, Ricardo Ribalda wrote:
> > > Friendly ping
> >
> > Empty pings provide no context at all :(
> 
> Do you mean that I'd rather left the whole patch as context, or that I
> should provide a reason for the ping?

Both, as it is, I have no idea of what you are asking for here with a
one line blank email.

> Let me try again:
> 
> Is there any change needed for
> https://lore.kernel.org/linux-spdx/2025040417-aspire-relenting-5462@gregkh/T/#t
> 
> that was sent for review over a month ago?

It's in the very-long review queue on my end, sorry.  Give us some
change to catch up.  While waiting, please feel free to help out in
reviewing changes from other people so that your changes bubble up to
the top.

thanks,

greg k-h