tests/testlib/sigpipe-worker.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Mon, 12 Jul 2021 03:30:04 +0200
changeset 47618 27ff81547d35
child 48875 6000f5b25c9b
permissions -rwxr-xr-x
sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill Instead of using sophisticated logics with thread a non blocking pipes, we simply spawn two new process in charge of reading the pipe and sending the result to the client. When it is time to cut the pipe we violently kill them without any remorse. This close the pipe regardless of any in progress `os.read` call. Ironically this is the very same things as what the initial shell setup was doing, but in Python. This makes the test pass run properly on Windows. This also reveal that the Windows behavior is broken as the transaction is not properly rollback. However this is an adventure for another time. Making the test behave properly was enough effort. Differential Revision: https://phab.mercurial-scm.org/D11087
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47618
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     1
#!/usr/bin/env python3
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
#
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
# This is literally `cat` but in python, one char at a time.
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     4
#
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
# see sigpipe-remote.py for details.
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
from __future__ import print_function
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     7
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
import io
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
import os
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
import sys
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
if isinstance(sys.stdout.buffer, io.BufferedWriter):
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
    print('SIGPIPE-WORKER: script need unbuffered output', file=sys.stderr)
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    15
    sys.exit(255)
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
while True:
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
    c = os.read(sys.stdin.fileno(), 1)
27ff81547d35 sigpipe-remote: simply delegate pipe forwarding to subprocess we can kill
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
    os.write(sys.stdout.fileno(), c)