Repository: streamlink/streamlink

Issue:
Socket is not connected error when closing currently open stream with VLC
### Checklist

- [x] This is a bug report.
- [ ] This is a feature request.
- [ ] This is a plugin (improvement) request.
- [ ] I have read the contribution guidelines.

### Description

Every time I close a stream that was playing in VLC, I get the following error:

```
[cli][info] Closing currently open stream...
Traceback (most recent call last):
  File "/usr/local/bin/streamlink", line 11, in <module>
    load_entry_point('streamlink==0.6.0', 'console_scripts', 'streamlink')()
  File "/usr/local/lib/python2.7/site-packages/streamlink_cli/main.py", line 1027, in main
    handle_url()
  File "/usr/local/lib/python2.7/site-packages/streamlink_cli/main.py", line 502, in handle_url
    handle_stream(plugin, streams, stream_name)
  File "/usr/local/lib/python2.7/site-packages/streamlink_cli/main.py", line 380, in handle_stream
    return output_stream_http(plugin, streams)
  File "/usr/local/lib/python2.7/site-packages/streamlink_cli/main.py", line 192, in output_stream_http
    server.close()
  File "/usr/local/lib/python2.7/site-packages/streamlink_cli/utils/http_server.py", line 116, in close
    self.socket.shutdown(2)
  File "/usr/local/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 57] Socket is not connected
```

This has been happening to me since 0.4.0, but I haven't had a chance to report it earlier. I've only been watching streams on Twtich so I have no idea if other services are affected by this too.

Issue #604 might be something similar, but the error is quite different although some parts of the backtrace are similar.

### Expected / Actual behavior

Expected: No error when closing the stream.
Actual: The above error happens.

### Reproduction steps / Explicit stream URLs to test

1. Load a Twitch stream with VLC as the player.
2. Close VLC.

This happens regardless of if the stream was still running when VLC is closed or if the stream already ended and VLC is not playing anything.

### Environment details

Operating system and version: FreeBSD 11.0-RELEASE-p8
Streamlink and Python version: Streamlink 0.6.0, Python 2.7.13
VLC version: 2.2.5.1

My .streamlinkrc file contains the following (excluding my Twitch OAuth token):

```
player-continuous-http
default-stream=best
hls-segment-threads=10
```


Discussion:
Hmm very strange, I wonder if this is a Python 2.7 issue. I'm running the Windows release on a Windows 10 machine with VLC 2.2.5.1, same settings and no errors when I close VLC directly, but that uses a more recent Python version.

Can you try to spin a python 3X virtualenv locally, install Streamlink there, and then see if you encounter the same problem with these settings? Thanks for the detailed report by the way.
Yeah, it might take me a bit to do that though. I'll try to get it done ASAP.
I tested streamlink with python 3.6, and when I close an active Twitch stream, I do not get the backtrace like I do when using python 2.7.
@CyberBotX Awesome thanks for confirming, I'll mark this as a bug.
Confirmed. Affects Python 2.7 only; Python 3.3, 3.4, 3.5, 3.6, and 3.7-dev all behave as expected. 

Resolving patch:
diff --git a/src/streamlink_cli/utils/http_server.py b/src/streamlink_cli/utils/http_server.py
--- a/src/streamlink_cli/utils/http_server.py
+++ b/src/streamlink_cli/utils/http_server.py
@@ -114,6 +114,6 @@ def close(self, client_only=False):
         if not client_only:
             try:
                 self.socket.shutdown(2)
-            except OSError:
+            except (OSError, socket.error):
                 pass
             self.socket.close()


Test patch: