Mercurial > hg
annotate contrib/showstack.py @ 45095:8e04607023e5
procutil: ensure that procutil.std{out,err}.write() writes all bytes
Python 3 offers different kind of streams and it’s not guaranteed for all of
them that calling write() writes all bytes.
When Python is started in unbuffered mode, sys.std{out,err}.buffer are
instances of io.FileIO, whose write() can write less bytes for
platform-specific reasons (e.g. Linux has a 0x7ffff000 bytes maximum and could
write less if interrupted by a signal; when writing to Windows consoles, it’s
limited to 32767 bytes to avoid the "not enough space" error). This can lead to
silent loss of data, both when using sys.std{out,err}.buffer (which may in fact
not be a buffered stream) and when using the text streams sys.std{out,err}
(I’ve created a CPython bug report for that:
https://bugs.python.org/issue41221).
Python may fix the problem at some point. For now, we implement our own wrapper
for procutil.std{out,err} that calls the raw stream’s write() method until all
bytes have been written. We don’t use sys.std{out,err} for larger writes, so I
think it’s not worth the effort to patch them.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Fri, 10 Jul 2020 12:27:58 +0200 |
parents | 2372284d9457 |
children | 6000f5b25c9b |
rev | line source |
---|---|
26123
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 # showstack.py - extension to dump a Python stack trace on signal |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 # |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 # binds to both SIGQUIT (Ctrl-\) and SIGINFO (Ctrl-T on BSDs) |
41548
6dae1f31c6c9
showstack: use raw docstring
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40036
diff
changeset
|
4 r"""dump stack trace when receiving SIGQUIT (Ctrl-\) or SIGINFO (Ctrl-T on BSDs) |
35656
c9eb92fb87b7
showstack: add an extension docstring
Boris Feld <boris.feld@octobus.net>
parents:
28522
diff
changeset
|
5 """ |
26123
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 |
40036
acf5dbe39478
showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents:
35656
diff
changeset
|
7 from __future__ import absolute_import, print_function |
28522
f2fe7b199bb4
showstack: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
26123
diff
changeset
|
8 import signal |
f2fe7b199bb4
showstack: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
26123
diff
changeset
|
9 import sys |
f2fe7b199bb4
showstack: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
26123
diff
changeset
|
10 import traceback |
26123
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41548
diff
changeset
|
12 |
26123
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 def sigshow(*args): |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 sys.stderr.write("\n") |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 traceback.print_stack(args[1], limit=10, file=sys.stderr) |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 sys.stderr.write("----\n") |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41548
diff
changeset
|
18 |
40036
acf5dbe39478
showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents:
35656
diff
changeset
|
19 def sigexit(*args): |
acf5dbe39478
showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents:
35656
diff
changeset
|
20 sigshow(*args) |
acf5dbe39478
showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents:
35656
diff
changeset
|
21 print('alarm!') |
acf5dbe39478
showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents:
35656
diff
changeset
|
22 sys.exit(1) |
acf5dbe39478
showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents:
35656
diff
changeset
|
23 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41548
diff
changeset
|
24 |
26123
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 def extsetup(ui): |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
26 signal.signal(signal.SIGQUIT, sigshow) |
40036
acf5dbe39478
showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents:
35656
diff
changeset
|
27 signal.signal(signal.SIGALRM, sigexit) |
26123
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 try: |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 signal.signal(signal.SIGINFO, sigshow) |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 except AttributeError: |
bdac264e5ed4
contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
31 pass |