Incorrect file input can cause decryption to hang

Issue #39 resolved
Randy Syring
created an issue

I recently ran some code like this:

with pathlib.Path(encrypted_fpath).open() as encrypted_fo:
            gpg.decrypt_file(encrypted_fo, always_trust=True, output=target_fpath)

Which hung the process.

I saw this output:

Exception in thread Thread-12:
Traceback (most recent call last):
  File "/opt/python34/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/opt/python34/lib/python3.4/threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "/home/rsyring/.virtualenvs/hllapi/lib/python3.4/site-packages/gnupg.py", line 127, in _copy_data
    data = instream.read(1024)
  File "/home/rsyring/.virtualenvs/hllapi/lib/python3.4/codecs.py", line 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0: invalid start byte

I was able to adjust my call to open (...open('rb')) in order to avoid this exception, but IMO, the main script should also exit with an exception, rather than hanging, if this situation occurs.

Comments (4)

  1. Ian Denhardt

    We have an unqualified except: in that code that says it's there for broken pipe errors. I bet it's erroneously catching this too, preventing the exception from propagating. I'll take a wack at appropriately narrowing that.

  2. Ian Denhardt

    Okay, so my initial guess was wrong --- didn't read very carefully. I have found the bug though, and have a fix that I'll submit once I've cleaned it up properly. The problem is basically that when that exception gets thrown, the calls to close the output at the end don't ever get run, and so the thread trying to read from the other end of the pipe just blocks. I'll submit a pr soon.

  3. Log in to comment