Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Fix for error message on program exit #25

Merged
merged 2 commits into from
Jan 31, 2018

Conversation

karthiknadig
Copy link
Member

No description provided.

ptvsd/wrapper.py Outdated
@@ -700,6 +717,8 @@ def start_server(port):
server_thread.daemon = True
server_thread.start()

atexit.register(lambda : proc.close())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a lambda here anymore - you can just pass proc.close directly.

ptvsd/wrapper.py Outdated
def __init__(self):
self.prog_exit_code = 0
self._sys_exit = sys.exit
sys.exit = self.exit_hook
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this approach is that it's not going to be particularly reliable. Suppose someone did from sys import exit - now they have a copy of what sys.exit was as a global or local variable at the time of the import, and if we haven't detoured yet, changing sys.exit inside the module isn't going to change that copy. Also, Python extensions can exit the process too, and they wouldn't invoke sys.exit (but rather the native function that backs it).

I'm not sure there's a better approach though. @brettcannon, can you think of anything? Basically we need to somehow capture the exit code of the process - or rather what we think said code will be - before it exits (because the code used to report the exit lives in that process).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys.exit() just raises SystemExit, so if you can grab that and check the exit code it's carrying that would be more reliable. (Maybe register something using the atexit module and then see whether SystemExit is set?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, look at sys.exc_info()? I just tried it, and looks like it's already cleared by the time atexit functions are running.

For launch scenarios, we could trap SystemExit at our entry point - provided that pydevd doesn't catch it first? Need to test whether this is feasible. For attach, though, this won't work, because user code is already running by the time it happens.

OTOH, the old debugger doesn't report exit code correctly in attach scenario, either, so it could be good enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I was thinking of sys.exc_info().

ptvsd/wrapper.py Outdated
def exit_hook(self, code=0):
self.prog_exit_code = code
self._sys_exit(code)
__ptvsd_sys_exit_code = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just make the variable public, and set and read it directly, even across the module boundary - in Python, it's idiomatic to skip accessor methods if all they do is read & write.

@karthiknadig karthiknadig merged commit a9ebc92 into microsoft:master Jan 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants