# Buggy code
def run_wsgi(self):
    if self.headers.get("Expect", "").lower().strip() == "100-continue":
        self.wfile.write(b"HTTP/1.1 100 Continue\r\n\r\n")

    self.environ = environ = self.make_environ()
    headers_set = []
    headers_sent = []

    def write(data):
        assert headers_set, "write() before start_response"
        if not headers_sent:
            status, response_headers = headers_sent[:] = headers_set
            try:
                code, msg = status.split(None, 1)
            except ValueError:
                code, msg = status, ""
            code = int(code)
            self.send_response(code, msg)
            header_keys = set()
            for key, value in response_headers:
                self.send_header(key, value)
                key = key.lower()
                header_keys.add(key)
            if not (
                "content-length" in header_keys
                or environ["REQUEST_METHOD"] == "HEAD"
                or code < 200
                or code in (204, 304)
            ):
                self.close_connection = True
                self.send_header("Connection", "close")
            if "server" not in header_keys:
                self.send_header("Server", self.version_string())
            if "date" not in header_keys:
                self.send_header("Date", self.date_time_string())
            self.end_headers()

        assert isinstance(data, bytes), "applications must write bytes"
        self.wfile.write(data)
        self.wfile.flush()

    def start_response(status, response_headers, exc_info=None):
        if exc_info:
            try:
                if headers_sent:
                    reraise(*exc_info)
            finally:
                exc_info = None
        elif headers_set:
            raise AssertionError("Headers already set")
        headers_set[:] = [status, response_headers]
        return write

    def execute(app):
        application_iter = app(environ, start_response)
        try:
            for data in application_iter:
                write(data)
            if not headers_sent:
                write(b"")
        finally:
            if hasattr(application_iter, "close"):
                application_iter.close()
            application_iter = None

    try:
        execute(self.server.app)
    except (_ConnectionError, socket.timeout) as e:
        self.connection_dropped(e, environ)
    except Exception:
        if self.server.passthrough_errors:
            raise
        from .debug.tbtools import get_current_traceback

        traceback = get_current_traceback(ignore_system_exceptions=True)
        try:
            # if we haven't yet sent the headers but they are set
            # we roll back to be able to set them again.
            if not headers_sent:
                del headers_set[:]
            execute(InternalServerError())
        except Exception:
            pass
        self.server.log("error", "Error on request:\n%s", traceback.plaintext)


# Traceback (ssl.SSLEOFError)
Error on request:
Traceback (most recent call last):
File "/home/relent95/.local/lib/python3.5/site-packages/werkzeug/serving.py", line 303, in run_wsgi
execute(self.server.app)
File "/home/relent95/.local/lib/python3.5/site-packages/werkzeug/serving.py", line 294, in execute
write(data)
File "/home/relent95/.local/lib/python3.5/site-packages/werkzeug/serving.py", line 275, in write
self.wfile.write(data)
File "/usr/lib/python3.5/socket.py", line 593, in write
return self._sock.send(b)
File "/usr/lib/python3.5/ssl.py", line 861, in send
return self._sslobj.write(data)
File "/usr/lib/python3.5/ssl.py", line 586, in write
return self._sslobj.write(data)
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:1848)

# Fixed code
def run_wsgi(self):
    if self.headers.get("Expect", "").lower().strip() == "100-continue":
        self.wfile.write(b"HTTP/1.1 100 Continue\r\n\r\n")

    self.environ = environ = self.make_environ()
    headers_set = []
    headers_sent = []

    def write(data):
        assert headers_set, "write() before start_response"
        if not headers_sent:
            status, response_headers = headers_sent[:] = headers_set
            try:
                code, msg = status.split(None, 1)
            except ValueError:
                code, msg = status, ""
            code = int(code)
            self.send_response(code, msg)
            header_keys = set()
            for key, value in response_headers:
                self.send_header(key, value)
                key = key.lower()
                header_keys.add(key)
            if not (
                "content-length" in header_keys
                or environ["REQUEST_METHOD"] == "HEAD"
                or code < 200
                or code in (204, 304)
            ):
                self.close_connection = True
                self.send_header("Connection", "close")
            if "server" not in header_keys:
                self.send_header("Server", self.version_string())
            if "date" not in header_keys:
                self.send_header("Date", self.date_time_string())
            self.end_headers()

        assert isinstance(data, bytes), "applications must write bytes"
        if data:
            # Only write data if there is any to avoid Python 3.5 SSL bug
            self.wfile.write(data)
        self.wfile.flush()

    def start_response(status, response_headers, exc_info=None):
        if exc_info:
            try:
                if headers_sent:
                    reraise(*exc_info)
            finally:
                exc_info = None
        elif headers_set:
            raise AssertionError("Headers already set")
        headers_set[:] = [status, response_headers]
        return write

    def execute(app):
        application_iter = app(environ, start_response)
        try:
            for data in application_iter:
                write(data)
            if not headers_sent:
                write(b"")
        finally:
            if hasattr(application_iter, "close"):
                application_iter.close()
            application_iter = None

    try:
        execute(self.server.app)
    except (_ConnectionError, socket.timeout) as e:
        self.connection_dropped(e, environ)
    except Exception:
        if self.server.passthrough_errors:
            raise
        from .debug.tbtools import get_current_traceback

        traceback = get_current_traceback(ignore_system_exceptions=True)
        try:
            # if we haven't yet sent the headers but they are set
            # we roll back to be able to set them again.
            if not headers_sent:
                del headers_set[:]
            execute(InternalServerError())
        except Exception:
            pass
        self.server.log("error", "Error on request:\n%s", traceback.plaintext)