Skip to content

fix(mt5client): survive a wedged SDK call and don't wedge the API - #26

Open
Marinski wants to merge 2 commits into
psyb0t:masterfrom
Marinski:fix/mt5client-wedge
Open

Marinski wants to merge 2 commits into
psyb0t:masterfrom
Marinski:fix/mt5client-wedge

Conversation

@Marinski

@Marinski Marinski commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #23.

  • single-flight guard: while a timed-out call is still inside the SDK, new calls raise MT5Wedged (503 + Retry-After) instead of spawning another thread; restart_terminal's own reconnect is exempt.
  • instrumentation: /ping reports the live SDK worker count and oldest age; every TIMEOUT/WEDGED line carries mode, path and worker count.
  • wedge watchdog: leaves the process (code 75) once a worker has been stuck for MT5_WEDGE_EXIT_SECONDS (180 s), deferring while a backtest holds RUN_LOCK, up to a 30 min ceiling.
  • restart_terminal skips mt5.shutdown while wedged.
  • also pass only the login fields to init_mt5 — a config account carrying extra keys otherwise TypeErrors and every SDK route on it returns 500.

Tests: tests/test_wedge_watchdog.py, plus added tests/test_mt5client.py cases.

@Marinski
Marinski force-pushed the fix/mt5client-wedge branch from d28fbef to 63be1a8 Compare October 2, 2026 08:54
…ng the API

A timed-out SDK call leaves its worker thread inside the SDK, and the
global lock then lets the next request call in beside it; each later
hang leaks another thread until waitress's queue saturates and even
/ping takes an hour (2026-08-23).

- Instrument: live SDK worker count and oldest age on /ping; every
  TIMEOUT/WEDGED log line carries mode, path and worker count.
- Single-flight guard: while a timed-out call is still inside the SDK,
  new calls raise MT5Wedged (503 + Retry-After) instead of starting a
  thread. restart_terminal's own reconnect is exempt.
- Wedge watchdog thread in every mode: exit the process (code 75) once
  a worker has been stuck MT5_WEDGE_EXIT_SECONDS (180 s), deferring
  while a backtest holds RUN_LOCK, up to a 30 min ceiling.
- restart_terminal skips mt5.shutdown while wedged.

The relaunch loop that brings an exited process back is a local
api_runner.bat hook applied by the backtester's deploy-mt5-scripts.sh.
config.yaml accounts can carry extra keys (the backtest manager's
symbol_map); init_mt5(**account) raised TypeError on them, so every
SDK route on such an account returned 500.
@Marinski
Marinski force-pushed the fix/mt5client-wedge branch from 63be1a8 to 67e2bf4 Compare October 2, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A timed-out MT5 SDK call leaks a worker thread and can wedge the whole API

1 participant