Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with AutoInterrupt in conjunction with sys.exit on python 3.5 #463

Closed
Heiko-san opened this issue Jun 6, 2016 · 21 comments
Closed

Comments

@Heiko-san
Copy link

Heiko-san commented Jun 6, 2016

The destructor of the cmd class (actually AutoInterrupt) seems to throw some errors using python 3.5 on Arclinux:

Exception ignored in: <bound method Git.AutoInterrupt.__del__ of <git.cmd.Git.AutoInterrupt object at 0x7f7c2b101d68>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/git/cmd.py", line 294, in __del__
TypeError: 'NoneType' object is not callable
Exception ignored in: <bound method Git.AutoInterrupt.__del__ of <git.cmd.Git.AutoInterrupt object at 0x7f7c2b101cc0>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/git/cmd.py", line 294, in __del__
TypeError: 'NoneType' object is not callable

It seems this error is triggered by a call to sys.exit(0) at the end of my script, since it doesn't appear if I comment it out.
However I couldn't reproduce this behavior with python 2.7, here sys.exit(0) seems to be working with the module correctly.

What I do is simply clone a repo, add some files commit and push them to a remote.

...
repo = Repo.clone_from(target_git_path, target_temp_dir)
...
repo.index.add([file])
...
repo.index.commit('Initial commit')
...
refspec='refs/heads/{0}:refs/heads/{0}'.format(repo.active_branch) # this will fix error if user has set "push.default = matching"
repo.remotes.origin.push(refspec=refspec)
@barry-scott
Copy link
Contributor

I recall from reading another isssue that the problem is that the process object is not being deleted
quickly under python3. This is because of the change in garbage collection between py2 and py3.

Looking at the code it looks like the fix is to explicitly del the process object once it is no longer needed.

This bug I suspect may happen on py2 depending on garbage collector config.

for example from cmd.py:965:

def __get_object_header(self, cmd, ref):
    cmd.stdin.write(self._prepare_ref(ref))
    cmd.stdin.flush()
    return self._parse_object_header(cmd.stdout.readline())

should be something along these lines:

def __get_object_header(self, cmd, ref):
    cmd.stdin.write(self._prepare_ref(ref))
    cmd.stdin.flush()
    result = self._parse_object_header(cmd.stdout.readline())
    del cmd
    return cmd

@Byron
Copy link
Member

Byron commented Jun 14, 2016

Which version of GitPython are you using ? It doesn't appear to be the latest one.
The line in question has recently be fixed to check for all the eventualities.
Thanks for the clarification.

@Byron
Copy link
Member

Byron commented Jul 23, 2016

Closed due to inactivity. Please comment to get the issue reopened.

@Byron Byron closed this as completed Jul 23, 2016
@barry-scott
Copy link
Contributor

Byron,

Unless you fixed the code this bug is still present in the code base.

Barry

@Byron
Copy link
Member

Byron commented Jul 30, 2016

@barry-scott Can you please answer the question in my last comment, or maybe even provide a test that triggers the issue to allow it to be fixed ? Otherwise this one would be open forever, and I try to avoid that. Thank you.

@Byron
Copy link
Member

Byron commented Jul 30, 2016

And I just noticed that you have not been the OP, yet it seemed you have information about the issue that might help resolving it.

@barry-scott
Copy link
Contributor

What I recall is that I noticed is the with python2 as soon as the object owning a process resource goes out of scope its is deleted. But in python3 the delete is delayed because of changes in garbage collection.
To force the clean up all objects that control a process resource must be delete explicitly using del proc

For example line 635 in cmd..py returns an instance of AutoInterrupt. All caller will have to be changed to del the returned object after use.

Unless this code change has been made this bug remains unfixed.

@Byron
Copy link
Member

Byron commented Aug 2, 2016

Thanks @barry-scott, I understand. It seems a context manager for the Cmd object would be an excellent addition to its API.

@Byron Byron added this to the v2.0.8 - Bugfixes milestone Aug 2, 2016
@barry-scott
Copy link
Contributor

I'm pondering if the cmd code should be ripped out and replaced en-mass.
Given the set of requirements that the cmd code has the current implementation is looking far to complex and hard to modify.

We now know that it needs to:

  • run a git command
  • allow all stdout to be processed in real time
  • allow all strerr to be processed in real time
  • allow prompts for input to be processed (for credentials)
  • use debug logging to verify operation
  • support Windows/macOS/Unix

I think that the above features for cmd can be implemented in a smaller and more obvious way
given all that has been learned with the current implementation.

(I wish you would setup a mailing list for GItPython to allow discussions)

@Byron
Copy link
Member

Byron commented Aug 3, 2016

@barry-scott I believe you are right - the code has been growing just to accommodate all the needs as they arose. This will eventually erode clarity unless some major refactoring or even a rewrite is done.

A good starting point would be to clearly separate the orthogonal parts. Something that comes to mind immediately is the part that streams objects through the git process (a hash goes in, an object comes out, the process is held alive permanently). Another obvious step could be to remove all reliance on the destructors and use context-managers or something like an idempotent release() method instead.

On the bright side, one can stake small steps in this endeavour, knowing that the massive reliance on the implementation will show any issues right away when running the tests, some of which might have to be adjusted at some point.

In any case, I find it most important to at most deprecate old parts of the API, so that existing clients have time to adjust. Ideally, they won't even have to, as many will just use it as a simple call wrapper.

When reading through the list of bullet points, one seems to be new in particular: allow prompts for input to be processed (for credentials). I wonder whether this actually has (or should ever) work through git itself. AFAIK, there is the GIT_ASKPASS environment variable just for that purpose.

This project had a mailing list before, and I closed it just because there was nothing happening. By now I do prefer using issues on github for all public communication. If you wish to go ahead with certain refactorings or improvements, you could open an issue for them and discuss the details there. Even though the process is not well defined, we will figure something out as we go, there isn't too many people active on this project after all. I hope that will work for you, as your contributions are greatly appreciated.

@barry-scott
Copy link
Contributor

On windows git credentials wincred prompts using the terminal stdin/stdout.
That has to be parsed out of the stream as in my case the console window if hidden.

I am working on a prototype of the process control and filtering the work is in:
/~https://github.com/barry-scott/scm-workbench/blob/master/Source/Git/Experiments/git_cmd.py

Its currently code is python3 only and the windows version is not sane.
Turns out I cannot get away with using half use subprocess Popen.
I will need to do the full CreateProcess Pattern, which will fix the python2
support I think.

The macos/linux version is showing promise.

python3 git_cmd.py git status

Barry

On 3 Aug 2016, at 04:27, Sebastian Thiel notifications@github.com wrote:

@barry-scott I believe you are right - the code has been growing just to accommodate all the needs as they arose. This will eventually erode clarity unless some major refactoring or even a rewrite is done.

A good starting point would be to clearly separate the orthogonal parts. Something that comes to mind immediately is the part that streams objects through the git process (a hash goes in, an object comes out, the process is held alive permanently). Another obvious step could be to remove all reliance on the destructors and use context-managers or something like an idempotent release() method instead.

On the bright side, one can stake small steps in this endeavour, knowing that the massive reliance on the implementation will show any issues right away when running the tests, some of which might have to be adjusted at some point.

In any case, I find it most important to at most deprecate old parts of the API, so that existing clients have time to adjust. Ideally, they won't even have to, as many will just use it as a simple call wrapper.

When reading through the list of bullet points, one seems to be new in particular: allow prompts for input to be processed (for credentials). I wonder whether this actually has (or should ever) work through git itself. AFAIK, there is the GIT_ASKPASS environment variable just for that purpose.

This project had a mailing list before, and I closed it just because there was nothing happening. By now I do prefer using issues on github for all public communication. If you wish to go ahead with certain refactorings or improvements, you could open an issue for them and discuss the details there. Even though the process is not well defined, we will figure something out as we go, there isn't too many people active on this project after all. I hope that will work for you, as your contributions are greatly appreciated.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@Byron
Copy link
Member

Byron commented Aug 4, 2016

Thanks for the heads-up. Looking at the code, I didn't see the usage of GIT_ASKPASS anywhere. It's just that I think it's the solution to the 'need to ask for credentials' problem, at least it is what the linked manual suggests.

@barry-scott
Copy link
Contributor

GIT_ASKPASS is half a solution. I could write a stub that using a named pipe to ask the main GUI for the answers. But that means the GUI must implement the credential storage. I guess I can figure out what git-credentials-wincred.exe does and fake being it.

@Byron
Copy link
Member

Byron commented Aug 5, 2016

I strongly believe GIT_ASKPASS works like SSH_ASKPASS, which really is nothing but a way to enter the password. It will still be managed by the respective credentials system, so there is nothing to be duplicated here.

@barry-scott
Copy link
Contributor

Yes you are right the credentials are stored after askpass returns them.
It is unfortunent that you get called twice once for username thne for password.
But nothing that a little state machine cannot solve.

I should have this askpass stuff integrated into scm woekbench soon.

Barry

On 5 Aug 2016, at 07:02, Sebastian Thiel notifications@github.com wrote:

I strongly believe GIT_ASKPASS works like SSH_ASKPASS, which really is nothing but a way to enter the password. It will still be managed by the respective credentials system, so there is nothing to be duplicated here.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@ankostis
Copy link
Contributor

@barry-scott you may take a fresh look at code after some major Windows issues have been resolved in #519 and remaining ones are marked for #525.

@barry-scott
Copy link
Contributor

@ankostis thanks for the heads up. I will test against trunk and check for regressions.

@Byron Byron modified the milestones: v2.0.9 - Bugfixes, v2.0.10 - Bugfixes Oct 16, 2016
@Byron Byron modified the milestones: v2.0.10 - Bugfixes, v2.0.9 - Bugfixes, v2.1.0 - proper windows support, v2.1.0 - better windows support, v2.1.1 - Bugfixes Oct 16, 2016
@Byron Byron modified the milestones: v2.1.1 - Bugfixes, v2.1.2 - Bugfixes Dec 8, 2016
@Byron Byron modified the milestones: v2.1.2 - Bugfixes, v2.1.3 - Bugfixes Mar 8, 2017
@Byron
Copy link
Member

Byron commented Mar 8, 2017

I am closing this issue as it had no interaction for more than 3 months. Please feel free to comment in case you need it to be reopened.

@Byron Byron closed this as completed Mar 8, 2017
@hobbitten123
Copy link

I am getting this error on apache with mod_wsgi on Redhat 7.2 with python 3.6 ... latest version of gitpython

[Fri Jun 08 13:03:17.584493 2018] [wsgi:error] [pid 21167] [client 10.53.9.7:33473] 2018-06-08 13:03:17,584 - ProcessGit - INFO - push:[]
[Fri Jun 08 13:07:38.056054 2018] [mpm_prefork:notice] [pid 21166] AH00170: caught SIGWINCH, shutting down gracefully
Exception ignored in: <bound method Repo.del of <git.Repo "/opt/xxx/cloud_automation/micro-services/scmprojects/resources/SRV-003106/tst8y9u/.git">>
Traceback (most recent call last):
File "/usr/venv/lib/python3.6/site-packages/git/repo/base.py", line 200, in del
NameError: name 'Exception' is not defined
Exception ignored in: <bound method Popen.del of <subprocess.Popen object at 0x7f0ec97652e8>>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/subprocess.py", line 761, in del
NameError: name 'ResourceWarning' is not defined
Exception ignored in: <bound method Popen.del of <subprocess.Popen object at 0x7f0ec97659e8>>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/subprocess.py", line 761, in del
NameError: name 'ResourceWarning' is not defined
Exception ignored in: <bound method Git.AutoInterrupt.del of <git.cmd.Git.AutoInterrupt object at 0x7f0ec9765390>>
Traceback (most recent call last):
File "/usr/venv/lib/python3.6/site-packages/git/cmd.py", line 372, in del
NameError: name 'getattr' is not defined

#-> /usr/sbin/httpd -V
Server version: Apache/2.4.6 (Red Hat Enterprise Linux)
Server built: Sep 17 2015 09:06:30
Server's Module Magic Number: 20120211:24
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/httpd"
-D SUEXEC_BIN="/usr/sbin/suexec"
-D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="conf/mime.types"
-D SERVER_CONFIG_FILE="conf/httpd.conf"

@Byron
Copy link
Member

Byron commented Jun 10, 2018

@hobbitten123 Depending on your usecase, you might also be able to eventually use /~https://github.com/Byron/grit-rs , which I just declared the spiritual successor of GitPython.

@sevendays
Copy link

Well, it happened to me too. Using python 3.7.3, gitpython 2.1.11, git 2.15.1.windows.1 on Windows10 and running everything from git bash GNU bash, version 4.4.12(1)-release (x86_64-pc-msys)

The app has many checks for existing files and uses sys.exit('error message') if some checks fail.

If everything goes well the app adds a list of files and commits them. Everything used to be smooth when there was only one file added and committed, but when I started having two files added and committed I got this error:


Exception ignored in: <function Git.AutoInterrupt.__del__ at 0x000001994C652488>
Traceback (most recent call last):
  File "C:\Users\luca.zamboni\AppData\Local\Programs\Python\Python37\lib\site-packages\git\cmd.py", line 368, in __del__
    if proc.poll() is not None:
  File "C:\Users\luca.zamboni\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 966, in poll
    return self._internal_poll()
  File "C:\Users\luca.zamboni\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1216, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid

The part of the app causing the error is this one:

    ############################################################################
    # Commit the review list file AND the review report file                   #
    ############################################################################

    # get the file relative to repo root
    WPRRlist_path_git = to_native_path_linux(os.path.relpath(os.path.realpath(WPRRlist_path), repo_rootpath)) # reviews list
    WPRR_path_git = to_native_path_linux(os.path.relpath(os.path.realpath(WPRR_newpath), repo_rootpath)) # review report
    repo.index.add([WPRRlist_path_git, WPRR_path_git])
    repo.index.commit(str(repo_path_git)+' review ' + str(result).upper() + 'ED; report: ' + str(WPRR_path_git) + '\n\n' + str(comment))
    print('Report moved to '+WPRR_path_git+' and review committed. You can still go back by discharging this commit.')

    ############################################################################
    # END                                                                      #
    ############################################################################
    exit(0)

The original with only one file added and committed was:


    ############################################################################
    # Commit the review list file                                              #
    ############################################################################

    # get the file relative to repo root
    review_repo_path = os.path.relpath(os.path.realpath(csvrevpath), repo_rootpath) # rel path
    repo.index.add([to_native_path_linux(review_repo_path)])
    repo.index.commit(str(repo_path_git)+' review ' + str(result).upper() + 'ED\n\n' + str(comment))
    print('Review committed. You can still go back by discharging this commit.')
    
    ############################################################################
    # END                                                                      #
    ############################################################################
    exit(0)

After reading #718 I added a manual deletion of the repo git object and everything works smooth.

    ############################################################################
    # Commit the review list file AND the review report file                   #
    ############################################################################

    # get the file relative to repo root
    WPRRlist_path_git = to_native_path_linux(os.path.relpath(os.path.realpath(WPRRlist_path), repo_rootpath)) # reviews list
    WPRR_path_git = to_native_path_linux(os.path.relpath(os.path.realpath(WPRR_newpath), repo_rootpath)) # review report
    repo.index.add([WPRRlist_path_git, WPRR_path_git])
    repo.index.commit(str(repo_path_git)+' review ' + str(result).upper() + 'ED; report: ' + str(WPRR_path_git) + '\n\n' + str(comment))
    print('Report moved to '+WPRR_path_git+' and review committed. You can still go back by discharging this commit.')

    ############################################################################
    # END                                                                      #
    ############################################################################
    repo.__del__()
    exit(0)

So as a workaround manually calling del() before calling exit(0) works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants