My script ends with sys.exit(0) but PyCharms says 'Process finished with exit code -1'

I have a Python script:

def main() -> int:

# do something
    return 0


if __name__ == '__main__':
sys.exit(main())

but in the Console window, when the script ends, it says:

Process finished with exit code -1

Am I doing something wrong?

6 comments
Comment actions Permalink

This example exits with code 0 for me as expected:

import sys


def main() -> int:
# do something
return 0


if __name__ == '__main__':
sys.exit(main())

 

Please try running it from the terminal without using IDE.

0
Comment actions Permalink

OK, I lied :-). My actual script is very long and complicated and performs some multi-threading, but the MT activity ends before the code reaches the 'return 0' statement.

To summarize:

1. My actual script, when run in the command line (no IDE), returns 0, as expected.

2. In the IDE, it shows -2, as I reported.

3. The brain-dead example that I provided returns 0 inside and outside of the IDE, so it's not helpful. Sigh.

0
Comment actions Permalink

it could be an issue related to garbage collection that involves object referencing each other whereby one tries to delete the other although this one was destructed already (known as global destruction fiasco).

so having ur code return 0 and this issue appearing after this it is likely it has to do with some garbage collection problem

0
Comment actions Permalink

Thanks for the insight. If your theory is correct, then it's definitely a bug in PyCharm.

0
Comment actions Permalink

No, i would say not pycharm related but a general issue even for others languages besides Python. E.g. Imagine two objects declared globaly in two different modules. How would garbage collection know what to destroy first? If these are referencing to eachother it can lead to problems. I have the same problem that my script runs until my last sys.exit(0) and fails immediately after. Its my personal assumption. Greets.

0
Comment actions Permalink

If the result is different in PyCharm vs running the same script without PyCharm, I suggest submitting an issue to https://youtrack.jetbrains.com/issues with code example.

0

Please sign in to leave a comment.