tests/test-sshserver.py
author Matt Harbison <matt_harbison@yahoo.com>
Tue, 21 Jan 2020 11:32:33 -0500
changeset 44320 43eea17ae7b3
parent 43076 2372284d9457
child 48966 6000f5b25c9b
permissions -rw-r--r--
lfs: fix the stall and corruption issue when concurrently uploading blobs We've avoided the issue up to this point by gating worker usage with an experimental config. See 10e62d5efa73, and the thread linked there for some of the initial diagnosis, but essentially some data was being read from the blob before an error occurred and `keepalive` retried, but didn't rewind the file pointer. So the leading data was lost from the blob on the server, and the connection stalled, trying to send more data than available. In trying to recreate this, I was unable to do so uploading from Windows to CentOS 7. But it reproduced every time going from CentOS 7 to another CentOS 7 over https. I found recent fixes in the FaceBook repo to address this[1][2]. The commit message for the first is: The KeepAlive HTTP implementation is bugged in it's retry logic, it supports reading from a file pointer, but doesn't support rewinding of the seek cursor when it performs a retry. So it can happen that an upload fails for whatever reason and will then 'hang' on the retry event. The sequence of events that get triggered are: - Upload file A, goes OK. Keep-Alive caches connection. - Upload file B, fails due to (for example) failing Keep-Alive, but LFS file pointer has been consumed for the upload and fd has been closed. - Retry for file B starts, sets the Content-Length properly to the expected file size, but since file pointer has been consumed no data will be uploaded, causing the server to wait for the uploaded data until either client or server reaches a timeout, making it seem as our mercurial process hangs. This is just a stop-gap measure to prevent this behavior from blocking Mercurial (LFS has retry logic). A proper solutions need to be build on top of this stop-gap measure: for upload from file pointers, we should support fseek() on the interface. Since we expect to consume the whole file always anyways, this should be safe. This way we can seek back to the beginning on a retry. I ported those two patches, and it works. But I see that `url._sendfile()` does a rewind on `httpsendfile` objects[3], so maybe it's better to keep this all in one place and avoid a second seek. We may still want the first FaceBook patch as extra protection for this problem in general. The other two uses of `httpsendfile` are in the wire protocol to upload bundles, and to upload largefiles. Neither of these appear to use a worker, and I'm not sure why workers seem to trigger this, or if this could have happened without a worker. Since `httpsendfile` already has a `close()` method, that is dropped. That class also explicitly says there's no `__len__` attribute, so that is removed too. The override for `read()` is necessary to avoid the progressbar usage per file. [1] https://github.com/facebookexperimental/eden/commit/c350d6536d90c044c837abdd3675185644481469 [2] https://github.com/facebookexperimental/eden/commit/77f0d3fd0415e81b63e317e457af9c55c46103ee [3] https://www.mercurial-scm.org/repo/hg/file/5.2.2/mercurial/url.py#l176 Differential Revision: https://phab.mercurial-scm.org/D7962
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     1
from __future__ import absolute_import, print_function
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     2
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     3
import io
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     4
import unittest
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     5
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     6
import silenttestrunner
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     7
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     8
from mercurial import (
35899
1bf5263fe5cc wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35772
diff changeset
     9
    wireprotoserver,
37785
b4d85bc122bd wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37123
diff changeset
    10
    wireprotov1server,
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    11
)
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    12
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    13
from mercurial.utils import procutil
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    14
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36252
diff changeset
    15
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    16
class SSHServerGetArgsTests(unittest.TestCase):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    17
    def testparseknown(self):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    18
        tests = [
36250
7a46f0735904 py3: add b'' to test-sshserver.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36103
diff changeset
    19
            (b'* 0\nnodes 0\n', [b'', {}]),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    20
            (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    21
                b'* 0\nnodes 40\n1111111111111111111111111111111111111111\n',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    22
                [b'1111111111111111111111111111111111111111', {}],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    23
            ),
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    24
        ]
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    25
        for input, expected in tests:
36250
7a46f0735904 py3: add b'' to test-sshserver.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36103
diff changeset
    26
            self.assertparse(b'known', input, expected)
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    27
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    28
    def assertparse(self, cmd, input, expected):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    29
        server = mockserver(input)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    30
        proto = wireprotoserver.sshv1protocolhandler(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    31
            server._ui, server._fin, server._fout
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    32
        )
37785
b4d85bc122bd wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37123
diff changeset
    33
        _func, spec = wireprotov1server.commands[cmd]
36252
3b3a987bbbaa wireprotoserver: move SSH server operation to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36250
diff changeset
    34
        self.assertEqual(proto.getargs(spec), expected)
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    35
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    36
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    37
def mockserver(inbytes):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    38
    ui = mockui(inbytes)
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    39
    repo = mockrepo(ui)
35899
1bf5263fe5cc wireprotoserver: move sshserver into module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35772
diff changeset
    40
    return wireprotoserver.sshserver(ui, repo)
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    41
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    42
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    43
class mockrepo(object):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    44
    def __init__(self, ui):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    45
        self.ui = ui
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    46
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    47
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    48
class mockui(object):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    49
    def __init__(self, inbytes):
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    50
        self.fin = io.BytesIO(inbytes)
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    51
        self.fout = io.BytesIO()
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    52
        self.ferr = io.BytesIO()
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    53
41285
cf8677cd7286 ui: proxy protect/restorestdio() calls to update internal flag
Yuya Nishihara <yuya@tcha.org>
parents: 37785
diff changeset
    54
    def protectfinout(self):
cf8677cd7286 ui: proxy protect/restorestdio() calls to update internal flag
Yuya Nishihara <yuya@tcha.org>
parents: 37785
diff changeset
    55
        return self.fin, self.fout
cf8677cd7286 ui: proxy protect/restorestdio() calls to update internal flag
Yuya Nishihara <yuya@tcha.org>
parents: 37785
diff changeset
    56
cf8677cd7286 ui: proxy protect/restorestdio() calls to update internal flag
Yuya Nishihara <yuya@tcha.org>
parents: 37785
diff changeset
    57
    def restorefinout(self, fin, fout):
cf8677cd7286 ui: proxy protect/restorestdio() calls to update internal flag
Yuya Nishihara <yuya@tcha.org>
parents: 37785
diff changeset
    58
        pass
cf8677cd7286 ui: proxy protect/restorestdio() calls to update internal flag
Yuya Nishihara <yuya@tcha.org>
parents: 37785
diff changeset
    59
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41285
diff changeset
    60
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    61
if __name__ == '__main__':
35772
7764ff13318e test-sshserver: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 35752
diff changeset
    62
    # Don't call into msvcrt to set BytesIO to binary mode
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36252
diff changeset
    63
    procutil.setbinary = lambda fp: True
35752
047581ddb6ce sshserver: add a couple of tests for argument parsing
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    64
    silenttestrunner.main(__name__)