Commit d70c3ee7 by PLN (Algolia)

fix(armada/serve): swallow client-disconnect on media Range seek

copyfile() threw BrokenPipeError/ConnectionResetError (Errno 104) when a
browser aborts/seeks a Range request mid-stream — normal for <audio>
scrubbing, but it spammed a traceback. Wrap both copy branches and close
the file handle in a finally. Verified: mid-stream abort leaves no
traceback and the server keeps serving (206 follow-up OK).
parent 92487ebe
...@@ -127,17 +127,26 @@ class RangeHandler(SimpleHTTPRequestHandler): ...@@ -127,17 +127,26 @@ class RangeHandler(SimpleHTTPRequestHandler):
return _LimitedReader(f, length) return _LimitedReader(f, length)
def copyfile(self, source, outputfile): def copyfile(self, source, outputfile):
if isinstance(source, _LimitedReader): # A client that seeks/aborts a media Range request mid-stream raises
remaining = source.remaining # BrokenPipeError/ConnectionResetError on the next write. That's normal
while remaining > 0: # for <audio> scrubbing, not a failure — swallow it (no traceback), but
chunk = source.read(min(64 * 1024, remaining)) # still close the file handle.
if not chunk: try:
break if isinstance(source, _LimitedReader):
outputfile.write(chunk) try:
remaining -= len(chunk) remaining = source.remaining
source.close() while remaining > 0:
else: chunk = source.read(min(64 * 1024, remaining))
super().copyfile(source, outputfile) if not chunk:
break
outputfile.write(chunk)
remaining -= len(chunk)
finally:
source.close()
else:
super().copyfile(source, outputfile)
except (BrokenPipeError, ConnectionResetError):
pass
class _LimitedReader: class _LimitedReader:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment