bounded_subprocess 2.3.0__py3-none-any.whl → 2.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of bounded_subprocess might be problematic. Click here for more details.

@@ -4,9 +4,8 @@ from .util import (
4
4
  Result,
5
5
  BoundedSubprocessState,
6
6
  SLEEP_BETWEEN_READS,
7
- write_loop_async,
7
+ write_nonblocking_async,
8
8
  _STDIN_WRITE_TIMEOUT,
9
- SLEEP_BETWEEN_WRITES,
10
9
  )
11
10
 
12
11
 
@@ -29,17 +28,16 @@ async def run(
29
28
  # async here? It's just the sleep between reads.
30
29
  state = BoundedSubprocessState(args, env, max_output_size, stdin_data is not None)
31
30
  if stdin_data is not None:
32
- ok = await write_loop_async(
33
- state.write_chunk,
34
- stdin_data.encode(),
35
- stdin_write_timeout if stdin_write_timeout is not None else 15,
36
- sleep_interval=SLEEP_BETWEEN_WRITES,
31
+ write_ok = await write_nonblocking_async(
32
+ fd=state.p.stdin,
33
+ data=stdin_data.encode(),
34
+ timeout_seconds=stdin_write_timeout if stdin_write_timeout is not None else 15,
37
35
  )
38
- if not ok:
39
- state.terminate()
40
- return Result(True, -1, "", "failed to write to stdin")
41
36
  await state.close_stdin_async(_STDIN_WRITE_TIMEOUT)
42
37
 
38
+ # Notice that we do not immediately terminate if the write fails. This allows
39
+ # us to read an error message from the process.
40
+
43
41
  # We sleep for 0.1 seconds in each iteration.
44
42
  max_iterations = timeout_seconds * 10
45
43
 
@@ -50,5 +48,9 @@ async def run(
50
48
  else:
51
49
  break
52
50
 
53
- return state.terminate()
51
+ result = state.terminate()
52
+ if stdin_data is not None and not write_ok:
53
+ result.exit_code = -1
54
+ result.stderr = result.stderr + "\nFailed to write all data to subprocess."
55
+ return result
54
56
 
@@ -94,6 +94,59 @@ async def write_loop_async(
94
94
  return True
95
95
 
96
96
 
97
+ async def can_write(fd):
98
+ """
99
+ Waits for the file descriptor to be writable.
100
+ """
101
+ future = asyncio.Future()
102
+ loop = asyncio.get_running_loop()
103
+ loop.add_writer(fd, future.set_result, None)
104
+ future.add_done_callback(lambda f: loop.remove_writer(fd))
105
+ await future
106
+
107
+
108
+ async def write_nonblocking_async(*, fd, data: bytes, timeout_seconds: int) -> bool:
109
+ """
110
+ Writes to a nonblocking file descriptor with the timeout.
111
+
112
+ Returns True if all the data was written. False indicates that there was
113
+ either a timeout or a broken pipe.
114
+ """
115
+ start_time_seconds = time.time()
116
+
117
+ # A slice, data[..], would create a copy. A memoryview does not.
118
+ mv = memoryview(data)
119
+ start = 0
120
+ while start < len(mv):
121
+ try:
122
+ # Write as much as possible without blocking.
123
+ written = fd.write(mv[start:])
124
+ if written is None:
125
+ written = 0
126
+ start = start + written
127
+ except BrokenPipeError:
128
+ return False
129
+ except BlockingIOError as exn:
130
+ if exn.errno != errno.EAGAIN:
131
+ # NOTE(arjun): I am not certain why this would happen. However,
132
+ # you are only supposed to retry on EAGAIN.
133
+ return False
134
+ # Some, but not all the bytes were written.
135
+ start = start + exn.characters_written
136
+
137
+ # Compute how much more time we have left.
138
+ wait_timeout = timeout_seconds - (time.time() - start_time_seconds)
139
+ # We are already past the deadline, so abort.
140
+ if wait_timeout <= 0:
141
+ return False
142
+ try:
143
+ await asyncio.wait_for(can_write(fd), wait_timeout)
144
+ except asyncio.TimeoutError:
145
+ # Deadline elapsed, so abort.
146
+ return False
147
+
148
+ return True
149
+
97
150
  class BoundedSubprocessState:
98
151
  """State shared between synchronous and asynchronous subprocess helpers."""
99
152
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bounded_subprocess
3
- Version: 2.3.0
3
+ Version: 2.3.1
4
4
  Summary: A library to facilitate running subprocesses that may misbehave.
5
5
  Project-URL: Homepage, https://github.com/arjunguha/bounded_subprocess
6
6
  Project-URL: Bug Tracker, https://github.com/arjunguha/bounded_subprocess
@@ -0,0 +1,10 @@
1
+ bounded_subprocess/__init__.py,sha256=L88cc8vG7GE11T0fF1-tMUIbRRlOmmlCqa6wopP3Ox8,1003
2
+ bounded_subprocess/bounded_subprocess.py,sha256=BkGDiPKuyl3Mftn6kUkdUbIfeXuLCYWDo57T1uAa-hI,1470
3
+ bounded_subprocess/bounded_subprocess_async.py,sha256=3QJ6GC3-ZD4-CWeHB13bOAtBrz6a955hwcZc6BRn2Os,1978
4
+ bounded_subprocess/interactive.py,sha256=4fG4NB3eN5rqssUpfjiEQL2F-S7eDBwB2Mw1gzCL3Qk,4149
5
+ bounded_subprocess/interactive_async.py,sha256=WKPA2XBnq_qMh5WijyoSOpg2iJcsYcQSXNGjv6IEEfA,1979
6
+ bounded_subprocess/util.py,sha256=we97jCA6kfHBfDu88hFKutUpyk_qg0Y363-_ScVhN8Y,8646
7
+ bounded_subprocess-2.3.1.dist-info/METADATA,sha256=iXgj3YNE1iLLw8E1-DS2mciLmTJUaVnGAjjQHSutRYM,2664
8
+ bounded_subprocess-2.3.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
+ bounded_subprocess-2.3.1.dist-info/licenses/LICENSE.txt,sha256=UVerBV0_1vMFt8QkaXuVnZVSlOiKDiBSieK5MNLy4Ls,1086
10
+ bounded_subprocess-2.3.1.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- bounded_subprocess/__init__.py,sha256=L88cc8vG7GE11T0fF1-tMUIbRRlOmmlCqa6wopP3Ox8,1003
2
- bounded_subprocess/bounded_subprocess.py,sha256=BkGDiPKuyl3Mftn6kUkdUbIfeXuLCYWDo57T1uAa-hI,1470
3
- bounded_subprocess/bounded_subprocess_async.py,sha256=hN323BOureEZFpT26-FmfK4K2WXZnP2VAc8mDqxOEeQ,1813
4
- bounded_subprocess/interactive.py,sha256=4fG4NB3eN5rqssUpfjiEQL2F-S7eDBwB2Mw1gzCL3Qk,4149
5
- bounded_subprocess/interactive_async.py,sha256=WKPA2XBnq_qMh5WijyoSOpg2iJcsYcQSXNGjv6IEEfA,1979
6
- bounded_subprocess/util.py,sha256=kYrq1Bcuvczx4oSxzVfztMDDHbsMUr68FyN6Bh-zhaU,6789
7
- bounded_subprocess-2.3.0.dist-info/METADATA,sha256=Sk2zX0VawscdNUHuFtbXCOqrc8qx0WxxcCj_UAt4nD4,2664
8
- bounded_subprocess-2.3.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
9
- bounded_subprocess-2.3.0.dist-info/licenses/LICENSE.txt,sha256=UVerBV0_1vMFt8QkaXuVnZVSlOiKDiBSieK5MNLy4Ls,1086
10
- bounded_subprocess-2.3.0.dist-info/RECORD,,