| 1 |
""" |
| 2 |
Windows-specific behavior of thread_aio's CRT interop |
| 3 |
(src/thread_aio/platform_io.rs). Everything here is skipped outright on |
| 4 |
non-Windows platforms. |
| 5 |
""" |
| 6 |
import ctypes |
| 7 |
import os |
| 8 |
import sys |
| 9 |
import threading |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
def test_invalid_parameter_handler_is_not_process_wide(tmp_path): |
| 15 |
"""The MSVC CRT invalid-parameter handler thread_aio installs around |
| 16 |
`_get_osfhandle` (needed so a closed/bogus fd raises a catchable |
| 17 |
error instead of aborting the process) must be scoped to the calling |
| 18 |
thread only, and restored immediately after - it must never replace |
| 19 |
the process-*wide* handler, which could otherwise silently change CRT |
| 20 |
error behavior for every other thread/extension in the process for as |
| 21 |
long as caio is imported. |
| 22 |
|
| 23 |
Installs a custom process-wide handler via ctypes first (simulating |
| 24 |
"some other code in the process already set one up"), then runs a |
| 25 |
normal thread_aio operation, then checks - still via ctypes - that the |
| 26 |
process-wide handler slot is untouched. |
| 27 |
""" |
| 28 |
if sys.platform != "win32": |
| 29 |
pytest.skip("Windows-only (thread_aio's CRT invalid-parameter-handler path)") |
| 30 |
|
| 31 |
from caio import thread_aio |
| 32 |
|
| 33 |
if thread_aio is None: |
| 34 |
pytest.skip("thread_aio backend not available on this platform") |
| 35 |
|
| 36 |
handler_t = ctypes.CFUNCTYPE( |
| 37 |
None, ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint, ctypes.c_size_t, |
| 38 |
) |
| 39 |
# The function lives in the modern Universal CRT (ucrtbase.dll), not the |
| 40 |
# legacy msvcrt.dll - matching what the Rust side actually links against |
| 41 |
# via a plain `extern "C"` declaration on current MSVC toolchains. |
| 42 |
ucrt = ctypes.CDLL("ucrtbase") |
| 43 |
ucrt._set_invalid_parameter_handler.restype = handler_t |
| 44 |
ucrt._set_invalid_parameter_handler.argtypes = [handler_t] |
| 45 |
|
| 46 |
@handler_t |
| 47 |
def my_handler(expression, function, file, line, reserved): |
| 48 |
pass |
| 49 |
|
| 50 |
previous = ucrt._set_invalid_parameter_handler(my_handler) |
| 51 |
try: |
| 52 |
path = tmp_path / "data.bin" |
| 53 |
path.write_bytes(b"hello") |
| 54 |
fd = os.open(str(path), os.O_RDONLY) |
| 55 |
try: |
| 56 |
ctx = thread_aio.Context(max_requests=4) |
| 57 |
op = thread_aio.Operation.read(5, fd, 0) |
| 58 |
done = threading.Event() |
| 59 |
op.set_callback(lambda _r: done.set()) |
| 60 |
ctx.submit(op) |
| 61 |
assert done.wait(timeout=5.0), "operation never completed" |
| 62 |
assert bytes(op.get_value()) == b"hello" |
| 63 |
finally: |
| 64 |
os.close(fd) |
| 65 |
|
| 66 |
# Read back the CURRENT process-wide handler (re-installing |
| 67 |
# my_handler as a side effect, which is fine - we restore the real |
| 68 |
# previous one in `finally` regardless). |
| 69 |
current = ucrt._set_invalid_parameter_handler(my_handler) |
| 70 |
current_ptr = ctypes.cast(current, ctypes.c_void_p).value |
| 71 |
my_handler_ptr = ctypes.cast(my_handler, ctypes.c_void_p).value |
| 72 |
assert current_ptr == my_handler_ptr, ( |
| 73 |
"a thread_aio operation must not replace the process-wide invalid-parameter handler" |
| 74 |
) |
| 75 |
finally: |
| 76 |
ucrt._set_invalid_parameter_handler(previous) |