comparison mercurial/sshpeer.py @ 38713:27391d74aaa2

ssh: avoid reading beyond the end of stream when using compression Compressed streams can be used as part of getbundle. The normal read() operation of bufferedinputpipe will try to fulfill the request exactly and can deadlock if the server sends less as it is done. At the same time, the bundle2 logic will stop reading when it believes it has gotten all parts of the bundle, which can leave behind end of stream markers as used by bzip2 and zstd. To solve this, introduce a new optional unbufferedread interface and provided it in bufferedinputpipe and doublepipe. If there is buffered data left, it will be returned, otherwise it will issue a single read request and return whatever it obtains. Reorganize the decompression handlers to try harder to read until the end of stream, especially if the requested read can already be fulfilled. Check for end of stream is messy with Python 2, none of the standard compression modules properly exposes it. At least with zstd and bzip2, decompressing will remember EOS and fail for empty input after the EOS has been seen. For zlib, the only way to detect it with Python 2 is to duplicate the decompressobj and force some additional data into it. The common handler can be further optimized, but works as PoC. Differential Revision: https://phab.mercurial-scm.org/D3937
author Joerg Sonnenberger <joerg@bec.de>
date Thu, 12 Jul 2018 18:46:10 +0200
parents 67dc32d4e790
children 089fc0db0954
comparison
equal deleted inserted replaced
38712:70a4289896b0 38713:27391d74aaa2
88 def write(self, data): 88 def write(self, data):
89 return self._call('write', data) 89 return self._call('write', data)
90 90
91 def read(self, size): 91 def read(self, size):
92 r = self._call('read', size) 92 r = self._call('read', size)
93 if size != 0 and not r:
94 # We've observed a condition that indicates the
95 # stdout closed unexpectedly. Check stderr one
96 # more time and snag anything that's there before
97 # letting anyone know the main part of the pipe
98 # closed prematurely.
99 _forwardoutput(self._ui, self._side)
100 return r
101
102 def unbufferedread(self, size):
103 r = self._call('unbufferedread', size)
93 if size != 0 and not r: 104 if size != 0 and not r:
94 # We've observed a condition that indicates the 105 # We've observed a condition that indicates the
95 # stdout closed unexpectedly. Check stderr one 106 # stdout closed unexpectedly. Check stderr one
96 # more time and snag anything that's there before 107 # more time and snag anything that's there before
97 # letting anyone know the main part of the pipe 108 # letting anyone know the main part of the pipe