Skip to content

[sys.stderr is None] Exceptions raised in threaded code are silenced #193

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

Closed
KirillMysnik opened this issue Apr 17, 2017 · 8 comments
Closed
Labels

Comments

@KirillMysnik
Copy link
Member

Windows, CS:GO

from threading import Thread


def func():
    print("Entering the function...")
    1 / 0
    print("Survived!!1")


Thread(target=func).start()

Output:

[SP] Loading plugin 'gamethread_test'...
Entering the function...
[SP] Successfully loaded plugin 'gamethread_test'.

No further tracebacks. Though if I put the whole code in the func in the try-except block and utilize traceback.print_exc, the traceback is printed:

from threading import Thread
from traceback import print_exc

def func():
    try:
        print("Entering the function...")
        1 / 0
        print("Survived!!1")
    except:
        print_exc()


Thread(target=func).start()
[SP] Loading plugin 'gamethread_test'...
Entering the function...
[SP] Successfully loaded plugin 'gamethread_test'.
Traceback (most recent call last):
  File "D:\SRCDS\csgo\csgo\addons\source-python\plugins\gamethread_test\gamethread_test.py", line 7, in func
    1 / 0
ZeroDivisionError: division by zero

Same with our dummy GameThread. Speaking of it, maybe we ditched it too soon? Seems like it can come around handy once again, this time to print tracebacks.

P.S. I'm centainly sure Source.Python used to print tracebacks from threads before, including thread ID.

@KirillMysnik
Copy link
Member Author

Tested a bit more, and it turns out that my sys.stderr is None. Globally, in both threaded and synchronous code.
But all other exceptions, as I might guess, are handled by Source.Python some other way as they all contain its header.
#175 - related?

@KirillMysnik KirillMysnik changed the title Exceptions raised in threaded code are silenced [sys.stderr is None] Exceptions raised in threaded code are silenced Apr 17, 2017
@satoon101
Copy link
Member

Very likely related to #175. Is it Windows 10?

@KirillMysnik
Copy link
Member Author

KirillMysnik commented Apr 17, 2017

Yes. I must add that the last time I've seen exceptions in Threads was the time I ran Windows 7.

@KirillMysnik
Copy link
Member Author

Okay, I did some more testing. And it really depends on how you launch your SRCDS. All my life I used to launch it with a regular windows shortcut.

Now I decided to try a command line with different redirecting options.

Plugin:

import sys

print("stderr: ", sys.stderr)
print("stdout: ", sys.stdout)

sys.stderr.write("Hello")
sys.stderr.flush()

Command line:

(Note 2> stderr.txt at the end)

D:\SRCDS\csgo\csgo>D:\SRCDS\csgo\srcds.exe -game csgo -console -port 27015 2> stderr.txt

Server console:

sp plugin load gamethread_test
[SP] Loading plugin 'gamethread_test'...
stderr:  <_io.TextIOWrapper name='<stderr>' mode='w' encoding='cp1251'>
stdout:  <__init__.setup_stdout_redirect.<locals>.StdoutRedirect object at 0x0EE0DFD0>
[SP] Successfully loaded plugin 'gamethread_test'.

See? Once I launched the server from a command line, stderr was successfully set to redirect to a file. But look at stdout! It's our StdoutRedirect from __init__.py - that means that stdout was set to None.

stderr.txt:

(Without flushing, strerr.txt remains empty)

d:\srcds\csgo\csgo\addons\source-python\packages\source-python\__init__.py:370: UserWarning: sys.stdout is None. All data will be redirected through core.console_message() instead. If you receive this warning, please notify us and tell us your operating system, game and Source.Python version. The information can be posted here: https://github.com/Source-Python-Dev-Team/Source.Python/issues/175. Source.Python should continue working, but we would like to figure out in which situations sys.stdout is None to be able to fix this issue instead of applying a workaround.
  'sys.stdout is None. All data will be redirected through '
Hello

So... I believe that the warning we print when redirecting stdout is invisible, because warnings go to stderr which is likely set to None on setups that already had stdout set to None.

@satoon101
Copy link
Member

Yeah, that makes sense, especially since we have not setup the exception/warning hooks at that point.

@Ayuto
Copy link
Member

Ayuto commented Apr 18, 2017

I guess then we just need to check sys.stderr as well and redirect it prior to printing the the warning.

Ayuto added a commit that referenced this issue Apr 22, 2017
@Ayuto
Copy link
Member

Ayuto commented Apr 22, 2017

@KirillMysnik Could you please check if the warning is now displayed?

@KirillMysnik
Copy link
Member Author

KirillMysnik commented Apr 23, 2017

I can confirm that the warning is now displayed. Windows 10, CS:GO DS - launched via shortcut (.lnk)

Also now I properly receive exceptions from the threaded code

Exception in thread Thread-3:
Traceback (most recent call last):
.....

@Ayuto Ayuto closed this as completed Apr 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants