# Buggy code
def render(self, request):
    """
    Render a request by forwarding it to the proxied server.

    Args:
        request (Request): Incoming request.

    Returns:
        not_done (char): Indicator to note request not yet finished.

    """
    # RFC 2616 tells us that we can omit the port if it's the default port,
    # but we have to provide it otherwise
    request.content.seek(0, 0)
    qs = urlparse.urlparse(request.uri)[4]
    if qs:
        rest = self.path + "?" + qs
    else:
        rest = self.path
    clientFactory = self.proxyClientFactoryClass(
        request.method,
        rest,
        request.clientproto,
        request.getAllHeaders(),
        request.content.read(),
        request,
    )
    clientFactory.noisy = False
    self.reactor.connectTCP(self.host, self.port, clientFactory)
    return NOT_DONE_YET


# Traceback (exceptions.RuntimeError)
2017-02-05T00:12:46-0500 [ProxyClient,client] Unhandled Error
Traceback (most recent call last):
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/application/app.py", line 310, in runReactorWithLogging
reactor.run()
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1195, in run
self.mainLoop()
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1207, in mainLoop
self.doIteration(t)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
self._disconnectSelectable(selectable, why, inRead)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 252, in _disconnectSelectable
selectable.readConnectionLost(f)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 272, in readConnectionLost
self.connectionLost(reason)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 478, in connectionLost
self._commonConnection.connectionLost(self, reason)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 292, in connectionLost
protocol.connectionLost(reason)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/web/http.py", line 486, in connectionLost
self.handleResponseEnd()
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/web/proxy.py", line 87, in handleResponseEnd
self.father.finish()
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/web/server.py", line 231, in finish
return http.Request.finish(self)
File "/home/tehom/arx/local/lib/python2.7/site-packages/twisted/web/http.py", line 863, in finish
"Request.finish called on a request after its connection was lost; "
exceptions.RuntimeError: Request.finish called on a request after its connection was lost; use Request.notifyFinish to keep track of this.

# Fixed code
def render(self, request):
    """
    Render a request by forwarding it to the proxied server.

    Args:
        request (Request): Incoming request.

    Returns:
        not_done (char): Indicator to note request not yet finished.

    """
    # RFC 2616 tells us that we can omit the port if it's the default port,
    # but we have to provide it otherwise
    request.content.seek(0, 0)
    qs = urlparse.urlparse(request.uri)[4]
    if qs:
        rest = self.path + "?" + qs
    else:
        rest = self.path
    clientFactory = self.proxyClientFactoryClass(
        request.method,
        rest,
        request.clientproto,
        request.getAllHeaders(),
        request.content.read(),
        request,
    )
    clientFactory.noisy = False
    self.reactor.connectTCP(self.host, self.port, clientFactory)
    # don't trigger traceback if connection is lost before request finish.
    request.notifyFinish().addErrback(lambda f: f.cancel())
    return NOT_DONE_YET