Skip to content

Commit 9c8d54e

Browse files
authored
fix: log named pipe disconnects and timeouts in FrontendRunner requests (#285)
On Windows, FrontendRunner._send_request logged "Failed to send {path} request" only for pywintypes.error. NamedPipeHelper raises its own PipeDisconnectedException and NamedPipeTimeoutError, which do not derive from pywintypes.error, so a broken pipe or read timeout during a heartbeat propagated without saying which request failed. Catch those exceptions as well, and include the elapsed time so the log shows whether the backend closed the connection at once or after its own request read timeout. Signed-off-by: Brian Axelson <86568017+baxeaz@users.noreply.github.com>
1 parent d9044ea commit 9c8d54e

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎src/openjd/adaptor_runtime/_background/frontend_runner.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
_FRONTEND_RUNNER_REQUEST_TIMEOUT: float = 5.0
3838

3939
if OSName.is_windows():
40-
from ...adaptor_runtime_client.named_pipe.named_pipe_helper import NamedPipeHelper
40+
from ...adaptor_runtime_client.named_pipe.named_pipe_helper import (
41+
NamedPipeHelper,
42+
NamedPipeTimeoutError,
43+
PipeDisconnectedException,
44+
)
4145
import pywintypes
4246

4347
_logger = logging.getLogger(__name__)
@@ -335,6 +339,7 @@ def _send_request(
335339
# This is used for aligning to the Linux's behavior in order to reuse the code in handler.
336340
# In linux, query string params will always be put in a list.
337341
params = {key: [value] for key, value in params.items()}
342+
start = time.monotonic()
338343
try:
339344
response = NamedPipeHelper.send_named_pipe_request(
340345
self.connection_settings.socket,
@@ -349,8 +354,11 @@ def _send_request(
349354
errmsg = f"Received unexpected HTTP status code {status}: {response['body']}"
350355
_logger.error(errmsg)
351356
raise HTTPError(response, errmsg)
352-
except pywintypes.error as e:
353-
_logger.error(f"Failed to send {path} request: {e}")
357+
# send_named_pipe_request raises its own types for disconnects and timeouts. The elapsed
358+
# time shows whether the backend closed the pipe at once or after its read timeout.
359+
except (pywintypes.error, PipeDisconnectedException, NamedPipeTimeoutError) as e:
360+
elapsed = time.monotonic() - start
361+
_logger.error(f"Failed to send {path} request after {elapsed:.2f}s: {e}")
354362
raise
355363
return response
356364
else: # pragma: is-windows

‎test/openjd/adaptor_runtime/unit/background/test_frontend_runner.py‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,17 +812,31 @@ def test_sends_request(
812812
mock_read_from_pipe.assert_called_once()
813813
assert response == json.loads(mock_response)
814814

815+
@pytest.mark.parametrize(
816+
"error_kind", ["pywintypes-error", "pipe-disconnected", "read-timeout"]
817+
)
815818
def test_raises_when_request_fails(
816819
self,
820+
error_kind: str,
817821
mock_read_from_pipe: MagicMock,
818822
mock_response: str,
819823
runner: FrontendRunner,
820824
caplog: pytest.LogCaptureFixture,
821825
):
822826
# GIVEN
823827
import pywintypes
828+
from openjd.adaptor_runtime_client.named_pipe.named_pipe_helper import (
829+
NamedPipeReadTimeoutError,
830+
PipeDisconnectedException,
831+
)
824832

825-
error_instance = pywintypes.error(1, "FunctionName", "An error message")
833+
error_instance = {
834+
"pywintypes-error": pywintypes.error(1, "FunctionName", "An error message"),
835+
"pipe-disconnected": PipeDisconnectedException(
836+
pywintypes.error(109, "ReadFile", "The pipe has been ended.")
837+
),
838+
"read-timeout": NamedPipeReadTimeoutError(5.0),
839+
}[error_kind]
826840
mock_read_from_pipe.side_effect = error_instance
827841
method = "GET"
828842
path = "/path"
@@ -834,12 +848,15 @@ def test_raises_when_request_fails(
834848
with patch.object(
835849
frontend_runner.NamedPipeHelper, "establish_named_pipe_connection"
836850
) as mock_establish_named_pipe_connection:
837-
with pytest.raises(pywintypes.error) as raised_exc:
851+
with pytest.raises(type(error_instance)) as raised_exc:
838852
runner._send_request(method, path)
839853

840854
# THEN
841855
assert raised_exc.value is error_instance
842-
assert f"Failed to send {path} request: " in caplog.text
856+
assert re.search(
857+
rf"Failed to send {path} request after \d+\.\d\ds: {re.escape(str(error_instance))}",
858+
caplog.text,
859+
)
843860
mock_write_to_pipe.assert_called_once_with(
844861
mock_establish_named_pipe_connection(), '{"method": "GET", "path": "/path"}'
845862
)

0 commit comments

Comments
 (0)