Repository: streamlink/streamlink

Issue:
python interpreter crash
Environment: 
windows 8.1 x64
streamlink: latest nightly build for windows



- First, i get a message "rtmpdump.exe has stopped working"
Like this:
![error img](http://thewindowsclub.thewindowsclubco.netdna-cdn.com/wp-content/uploads/2014/02/sqlstoped2.png)

- Then "Python has stopped working"

streamlink output:
```
[cli][info] Opening stream: high (rtmp)
[cli][debug] Pre-buffering 8192 bytes
[cli][error] Failed to read data from stream: Read timeout
Fatal Python error: could not acquire lock for <_io.BufferedReader name=5> at in
terpreter shutdown, possibly due to daemon threads

Thread 0x000014bc (most recent call first):
  File "C:\Program Files (x86)\Streamlink\pkgs\streamlink\stream\wrappers.py", line 67 in run
  File "threading.py", line 914 in _bootstrap_inner
  File "threading.py", line 882 in _bootstrap

Current thread 0x000012f8 (most recent call first):
  File "C:\Program Files (x86)\Streamlink\pkgs\streamlink\stream\wrappers.py", line 85 in stop
  File "C:\Program Files (x86)\Streamlink\pkgs\streamlink\stream\wrappers.py", line 105 in close
  File "C:\Program Files (x86)\Streamlink\pkgs\streamlink\stream\streamprocess.py", line 27 in close

```

I also have windows 7 x32 and error sometimes reproduced on win 8 only.


Discussion:
Does this only occur on the latest nightly build or the stable build as well? Also what site is this happening on? What does the command you are running look like?
- I never used stable build, can't say.
- I used my plugin from here: https://raw.githubusercontent.com/xkbd/streamlink/4271aa099ae10a66e6c74b825907f2f3737b10ac/src/streamlink/plugins/bongacams.py (copied it to pkgs\streamlink\plugins)
- streamlink https://site_name.domain/streampath
Plus in the config:
default-stream=best


I don't think that this is due to some specific plug-in. With any which use RTMPStream.
another reproduce:
`streamlink url --player="not-exists-file"`
The url should process plugin which will run rtmpdump.
@xkbd In my case is not reproducible:
![streamlink_player_not_found](https://cloud.githubusercontent.com/assets/13382316/21955299/3e85cf12-da47-11e6-88cd-9a9c6a5e4b87.JPG)

UPD: The url should process plugin which will run rtmpdump.
In my case:

```
[cli][info] Opening stream: live (rtmp)
[cli][debug] Pre-buffering 8192 bytes
[cli][info] Starting player: none
error: Failed to start player: none ([WinError 2] Не удается найти указанный файл)
Fatal Python error: could not acquire lock for <_io.BufferedReader name=5> at interpreter shutdown, possibly due to daemon threads

Thread 0x00001630 (most recent call first):
  File "c:\python35\lib\site-packages\streamlink\stream\wrappers.py", line 67 in run
  File "c:\python35\lib\threading.py", line 914 in _bootstrap_inner
  File "c:\python35\lib\threading.py", line 882 in _bootstrap

Current thread 0x00000b20 (most recent call first):
  File "c:\python35\lib\site-packages\streamlink\stream\wrappers.py", line 85 in stop
  File "c:\python35\lib\site-packages\streamlink\stream\wrappers.py", line 105 in close
  File "c:\python35\lib\site-packages\streamlink\stream\streamprocess.py", line 30 in close

```
@xkbd 

![streamlink_player_not_found_rtmp](https://cloud.githubusercontent.com/assets/13382316/21955450/33224f9e-da4a-11e6-924a-1a1ac5a08a79.JPG)

Edit: If you use `Streamlink.exe "vaughnlive.tv/MEGA_SIMPSON" best --player="not-exists-file"` what happens in your case?
streamlink from nightly build - OK

Streamlink from C:\Python35\Scripts\:
```
[cli][info] Found matching plugin vaughnlive for URL vaughnlive.tv/MEGA_SIMPSON
[plugin.vaughnlive][debug] Using swf url: http://vaughnlive.tv/4451818440/swf/VaughnSoftPlayer.swf
[plugin.vaughnlive][debug] Loading info url: http://mvn.vaughnsoft.net/video/edge/soon_depricated_Q2_2017-live_mega_simpson?0.1.1.782_661-661-0.6530276332369996
[cli][info] Available streams: live (worst, best)
[cli][info] Opening stream: live (rtmp)
[cli][debug] Pre-buffering 8192 bytes
[cli][info] Starting player: not-exists-file
error: Failed to start player: not-exists-file ([WinError 2] Не удается найти указанный файл)
Fatal Python error: could not acquire lock for <_io.BufferedReader name=5> at interpreter shutdown, possibly due to daemon threads

Thread 0x000018a0 (most recent call first):
  File "c:\python35\lib\site-packages\streamlink\stream\wrappers.py", line 67 in run
  File "c:\python35\lib\threading.py", line 914 in _bootstrap_inner
  File "c:\python35\lib\threading.py", line 882 in _bootstrap

Current thread 0x00001e74 (most recent call first):
  File "c:\python35\lib\site-packages\streamlink\stream\wrappers.py", line 85 in stop
  File "c:\python35\lib\site-packages\streamlink\stream\wrappers.py", line 105 in close
  File "c:\python35\lib\site-packages\streamlink\stream\streamprocess.py", line 30 in close
```

```
C:\Python35\Scripts>python --version
Python 3.5.2

C:\Python35\Scripts>streamlink.exe -V
streamlink.exe 0.2.0
```
@RosadinTV, can you try run streamlink.exe from python_dir\scripts?
```
C:\Users\user>python -c "from streamlink_cli.main import main;main()" "vaughnlive.tv/MEGA_SIMPSON" best --player="not-exists-file"
[cli][info] Found matching plugin vaughnlive for URL vaughnlive.tv/MEGA_SIMPSON
[plugin.vaughnlive][debug] Using swf url: http://vaughnlive.tv/4451818440/swf/VaughnSoftPlayer.swf
[plugin.vaughnlive][debug] Loading info url: http://mvn.vaughnsoft.net/video/edge/soon_depricated_Q2_2017-live_mega_simpson?0.1.1.782_685-68
5-0.8773321155576378
[cli][info] Available streams: live (worst, best)
[cli][info] Opening stream: live (rtmp)
[cli][debug] Pre-buffering 8192 bytes
[cli][info] Starting player: not-exists-file
error: Failed to start player: not-exists-file ([WinError 2] Не удается найти указанный файл)
Fatal Python error: could not acquire lock for <_io.BufferedReader name=5> at interpreter shutdown, possibly due to daemon threads

Thread 0x000017d4 (most recent call first):
  File "C:\Python35\lib\site-packages\streamlink\stream\wrappers.py", line 67 in run
  File "C:\Python35\lib\threading.py", line 914 in _bootstrap_inner
  File "C:\Python35\lib\threading.py", line 882 in _bootstrap

Current thread 0x00001074 (most recent call first):
  File "C:\Python35\lib\site-packages\streamlink\stream\wrappers.py", line 85 in stop
  File "C:\Python35\lib\site-packages\streamlink\stream\wrappers.py", line 105 in close
  File "C:\Python35\lib\site-packages\streamlink\stream\streamprocess.py", line 30 in close
```


When i use script all is well:
```
C:\Users\user>type test.py
from streamlink_cli.main import main
main()
C:\Users\user>python test.py "vaughnlive.tv/MEGA_SIMPSON" best --player="not-exists-file"
[cli][info] Found matching plugin vaughnlive for URL vaughnlive.tv/MEGA_SIMPSON
[plugin.vaughnlive][debug] Using swf url: http://vaughnlive.tv/4451818440/swf/VaughnSoftPlayer.swf
[plugin.vaughnlive][debug] Loading info url: http://mvn.vaughnsoft.net/video/edge/soon_depricated_Q2_2017-live_mega_simpson?0.1.1.782_974-97
4-0.23589034015397992
[cli][info] Available streams: live (worst, best)
[cli][info] Opening stream: live (rtmp)
[cli][debug] Pre-buffering 8192 bytes
[cli][info] Starting player: not-exists-file
error: Failed to start player: not-exists-file ([WinError 2] Не удается найти указанный файл)
```
@xkbd the error doesn't occur with the streamlink.exe that is installed by the nightly? 
Doesn't occur [in last case]. In case when rtmpdump crashes (my first message ) - occur.
streamlink.exe from windows installer package - runner streamlink-script.py. 
In my case when i run streamlink from python-script also all is well.

But if i run `python.exe -c` [code from streamlink-script.py]:
```
C:\Program Files\Streamlink>Python\python.exe -c "import sys, os;installdir = os.path.dirname('bin');pkgdir = os.path.join(installdir, 'pkgs');sys.path.insert(0, pkgdir);os.environ['PYTHONPATH'] = pkgdir + os.pathsep + os.environ.get('PYTHONPATH', '');from streamlink_cli.main import main;main()" "vaughnlive.tv/MEGA_SIMPSON" best --player="not-exists-file"
```
I get 'python fatal error'
```
C:\Program Files\Streamlink>Python\python.exe -c "import sys, os;installdir = os.path.dirname('bin');pkgdir = os.path.join(installdir, 'pkgs
');sys.path.insert(0, pkgdir);os.environ['PYTHONPATH'] = pkgdir + os.pathsep + os.environ.get('PYTHONPATH', '');from streamlink_cli.main imp
ort main;main()" "vaughnlive.tv/MEGA_SIMPSON" best --player="not-exists-file"
Failed to load plugin viagame:
  File "imp.py", line 234, in load_module
  File "imp.py", line 172, in load_source
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "pkgs\streamlink\plugins\viagame.py", line 7, in <module>
    from streamlink.plugin.api.support_plugin import viasat
  File "pkgs\streamlink\plugin\api\__init__.py", line 27, in __getattr__
    return load_support_plugin(name)
  File "pkgs\streamlink\plugin\api\support_plugin.py", line 39, in load_support_plugin
    raise ImportError("No module named '{0}'".format(name))
ImportError: No module named 'viasat'

[cli][info] Found matching plugin vaughnlive for URL vaughnlive.tv/MEGA_SIMPSON
[plugin.vaughnlive][debug] Using swf url: http://vaughnlive.tv/4451818440/swf/VaughnSoftPlayer.swf
[plugin.vaughnlive][debug] Loading info url: http://mvn.vaughnsoft.net/video/edge/soon_depricated_Q2_2017-live_mega_simpson?0.1.1.782_501-50
1-0.07308929867030278
[cli][info] Available streams: live (worst, best)
[cli][info] Opening stream: live (rtmp)
[cli][debug] Pre-buffering 8192 bytes
[cli][info] Starting player: not-exists-file
error: Failed to start player: not-exists-file ([WinError 2] Не удается найти указанный файл)
Fatal Python error: could not acquire lock for <_io.BufferedReader name=5> at interpreter shutdown, possibly due to daemon threads

Thread 0x000010ec (most recent call first):
  File "pkgs\streamlink\stream\wrappers.py", line 67 in run
  File "threading.py", line 914 in _bootstrap_inner
  File "threading.py", line 882 in _bootstrap

Current thread 0x00001d54 (most recent call first):
  File "pkgs\streamlink\stream\wrappers.py", line 85 in stop
  File "pkgs\streamlink\stream\wrappers.py", line 105 in close
  File "pkgs\streamlink\stream\streamprocess.py", line 30 in close

```
```
#foo.py
from streamlink_cli.main import main
main()
```
What's the difference between `python -c "from streamlink_cli.main import main;main()"` and `python foo.py` ?
Why in the first case the interpreter was falling and the second not?
@xkbd You are right, when i run Streamlink.exe (Located in Scripts folder after installing via pip) the crash occurs:
![streamlink_player_not_found_rtmp_error](https://cloud.githubusercontent.com/assets/13382316/21958626/3eb23a76-da91-11e6-9d33-7b0b4367d4de.JPG)
Are you using the most up go date version of pip?
```
>pip -V
pip 9.0.1 from c:\python35\lib\site-packages (python 3.5)
```
pip is not to blame
@beardypig Yes, but the crash only occurs in rtmp streams with a "not-exist" player
I tested in both portable and installable builds, no crash ocurred, only reproducible in pip.
I got another crash with streamlink.exe from windows package :)

```
C:\Program Files\Streamlink\bin>streamlink.exe "vaughnlive.tv/MEGA_SIMPSON" best
[cli][info] Found matching plugin vaughnlive for URL vaughnlive.tv/MEGA_SIMPSON
[plugin.vaughnlive][debug] Using swf url: http://vaughnlive.tv/4451818440/swf/VaughnSoftPlayer.swf
[plugin.vaughnlive][debug] Loading info url: http://mvn.vaughnsoft.net/video/edge/soon_depricated_Q2_2017-live_mega_simpson?0.1.1.782_530-53
0-0.6985136105910077
[cli][info] Available streams: live (worst, best)
[cli][info] Opening stream: live (rtmp)
[cli][debug] Pre-buffering 8192 bytes
[cli][error] Failed to read data from stream: Read timeout
Fatal Python error: could not acquire lock for <_io.BufferedReader name=5> at interpreter shutdown, possibly due to daemon threads

Thread 0x000017f8 (most recent call first):
  File "C:\Program Files\Streamlink\pkgs\streamlink\stream\wrappers.py", line 67 in run
  File "threading.py", line 914 in _bootstrap_inner
  File "threading.py", line 882 in _bootstrap

Current thread 0x000017a8 (most recent call first):
  File "C:\Program Files\Streamlink\pkgs\streamlink\stream\wrappers.py", line 85 in stop
  File "C:\Program Files\Streamlink\pkgs\streamlink\stream\wrappers.py", line 105 in close
  File "C:\Program Files\Streamlink\pkgs\streamlink\stream\streamprocess.py", line 30 in close
```
Steps to reproduce:
- `streamlink.exe "vaughnlive.tv/MEGA_SIMPSON" best`
- To obtain `read timeout` suspend rtmpdump.exe process


Resolving patch:
diff --git a/src/streamlink_cli/main.py b/src/streamlink_cli/main.py
--- a/src/streamlink_cli/main.py
+++ b/src/streamlink_cli/main.py
@@ -931,16 +931,14 @@ def main():
             # Close output
             if output:
                 output.close()
-
-            # Make sure current stream gets properly cleaned up
+            console.msg("Interrupted! Exiting...")
+        finally:
             if stream_fd:
-                console.msg("Interrupted! Closing currently open stream...")
                 try:
+                    console.logger.info("Closing currently open stream...")
                     stream_fd.close()
                 except KeyboardInterrupt:
                     sys.exit()
-            else:
-                console.msg("Interrupted! Exiting...")
     elif args.twitch_oauth_authenticate:
         authenticate_twitch_oauth()
     elif args.help:


Test patch: