tests/test-nointerrupt.t
author Augie Fackler <augie@google.com>
Wed, 27 Jun 2018 10:47:14 -0400
changeset 38526 313a940d49a3
child 38656 531f5e933e49
permissions -rw-r--r--
ui: add an uninterruptable context manager that can block SIGINT The blocking of SIGINT is not done by default, but my hope is that we will one day. This was inspired by Facebook's "nointerrupt" extension, which is a bit more heavy-handed than this (whole commands are treated as unsafe to interrupt). A future patch will enable this for varying bits of Mercurial that are performing unsafe operations. It's intentional that the KeyboardInterrupt is raised as the context manager exits: during the span of the context manager interrupting Mercurial could lead to data loss, but typically those spans are fairly narrow, so we can let the unsafe block complete and then terminate hg (which will leave the repo in a consistent state, even if it's not the user's desired state). .. api:: New context manager ``ui.uninterruptable()`` to mark portions of a command as potentially unsafe places to interrupt Mercurial with Control-C or similar. Differential Revision: https://phab.mercurial-scm.org/D3716
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38526
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
Dummy extension simulating unsafe long running command
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
  $ cat > sleepext.py <<EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
  > import time
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
  > import itertools
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
  > 
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
  > from mercurial import registrar
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
  > from mercurial.i18n import _
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
  > 
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
  > cmdtable = {}
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
  > command = registrar.command(cmdtable)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
  > 
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
  > @command(b'sleep', [], _(b'TIME'), norepo=True)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
  > def sleep(ui, sleeptime=b"1", **opts):
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
  >     with ui.uninterruptable():
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
  >         for _i in itertools.repeat(None, int(sleeptime)):
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
  >             time.sleep(1)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
  >         ui.warn(b"end of unsafe operation\n")
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
  >     ui.warn(b"%s second(s) passed\n" % sleeptime)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
  > EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
Kludge to emulate timeout(1) which is not generally available.
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
  $ cat > timeout.py <<EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
  > from __future__ import print_function
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
  > import argparse
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
  > import signal
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
  > import subprocess
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
  > import sys
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
  > import time
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
  > 
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
  > ap = argparse.ArgumentParser()
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
  > ap.add_argument('-s', nargs=1, default='SIGTERM')
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
  > ap.add_argument('duration', nargs=1, type=int)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
  > ap.add_argument('argv', nargs='*')
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
  > opts = ap.parse_args()
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
  > try:
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
  >     sig = int(opts.s[0])
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
  > except ValueError:
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
  >     sname = opts.s[0]
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
  >     if not sname.startswith('SIG'):
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
  >         sname = 'SIG' + sname
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
  >     sig = getattr(signal, sname)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
  > proc = subprocess.Popen(opts.argv)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
  > time.sleep(opts.duration[0])
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
  > proc.poll()
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
  > if proc.returncode is None:
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
  >     proc.send_signal(sig)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
  >     proc.wait()
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
  >     sys.exit(124)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
  > EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
Set up repository
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
  $ hg init repo
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
  $ cd repo
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
  $ cat >> $HGRCPATH << EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
  > [extensions]
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    56
  > sleepext = ../sleepext.py
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    57
  > EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    58
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    59
Test ctrl-c
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
  $ python $TESTTMP/timeout.py -s INT 1 hg sleep 2
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
  interrupted!
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
  [124]
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
  $ cat >> $HGRCPATH << EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
  > [experimental]
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
  > nointerrupt = yes
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
  > EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
  $ python $TESTTMP/timeout.py -s INT 1 hg sleep 2
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
  interrupted!
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
  [124]
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
  $ cat >> $HGRCPATH << EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
  > [experimental]
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
  > nointerrupt-interactiveonly = False
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
  > EOF
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
  $ python $TESTTMP/timeout.py -s INT 1 hg sleep 2
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
  shutting down cleanly
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    80
  press ^C again to terminate immediately (dangerous)
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    81
  end of unsafe operation
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    82
  interrupted!
313a940d49a3 ui: add an uninterruptable context manager that can block SIGINT
Augie Fackler <augie@google.com>
parents:
diff changeset
    83
  [124]