sjoerdk / dicomtrolley

Retrieve medical images via WADO, MINT, RAD69 and DICOM-QR

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WADO-RS download on plain session ConnectionResetError (10054) is not handled

sjoerdk opened this issue · comments

  • dicomtrolley version: 3.0.0-alpha
  • Python version: 3.7

Description

When performing longer wado-rs operations with a single requests Session object, a ConnectionResetError is raised by urllib3.
This bubbles up and is not handled by dicomtrolley which exits. It should be possible to just retry using a new connection.

What I Did

# start a wado-rs download of a longer (several minutes) download:
session = requests.Session()
wado_rs = WadoRS(session=session, url="http://server/dicomweb/")
wado_rs.datasets(StudyReference(study_uid='123')))  # study taking minutes to download

# then download a second study on the same object
wado_rs.datasets(StudyReference(study_uid='456')))  # any other study

Origin and route the exception takes:

Traceback (most recent call last):
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\urllib3\response.py", line 438, in _error_catcher
    yield
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\urllib3\response.py", line 767, in read_chunked
    chunk = self._handle_chunk(amt)
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\urllib3\response.py", line 720, in _handle_chunk
    returned_chunk = self._fp._safe_read(self.chunk_left)
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\http\client.py", line 628, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\requests\models.py", line 753, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\urllib3\response.py", line 572, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\urllib3\response.py", line 793, in read_chunked
    self._original_response.close()
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\urllib3\response.py", line 455, in _error_catcher
    raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\dicomtrolley\http.py", line 163, in read_next_chunk
    return next(self._bytes_iterator)
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\dicomtrolley\http.py", line 53, in __next__
    sized_chunk += next(self._chunk_iterator)
  File "C:\ProgramData\Miniconda3\envs\speedtest\lib\site-packages\requests\models.py", line 756, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

There was a very similar issue with rad69. This was worked around by lessening the server load by splitting downloads along series (#11). See 14eb3f4