crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Event loop construction under socket-blocking test guards fails on Windows (Network is disabled)

Open yuee3 opened this issue 1 month ago • 1 comments

Description

On Windows, every CrewAI test that emits an event (directly or via Flow/Crew construction) dies with RuntimeError: Network is disabled when the test session uses a socket-blocking guard such as pytest-recording's --block-network (which crewAI's own root addopts enable by default) or pytest-socket's --disable-socket.

Root cause

  1. CrewAIEventsBus._ensure_executor_initialized() (lib/crewai/src/crewai/events/event_bus.py) and _run_sync() (lib/crewai/src/crewai/utilities/file_store.py) construct asyncio event loops lazily.
  2. On Windows, asyncio builds every event loop's self-pipe with an emulated socket.socketpair() that performs a real connect() to 127.0.0.1 (proactor_events.py:787 → socket.py:643 csock.connect(...)).
  3. Socket-blocking guards patch socket.socket.connect and reject that internal, non-network self-pipe.

On Linux the self-pipe is built via os.socketpair() and never calls connect(), which is why the ubuntu-only CI never sees this.

Evidence

Local Windows 11 run of the full suite (pytest with the repo's own addopts):

  • 2224 failures with RuntimeError: Network is disabled; every traceback passes through event_bus.py ... _ensure_executor_initialized → asyncio.new_event_loop() → proactor_events.py: _make_self_pipe → socket.socketpair → pytest_recording/network.py: network_guard.
  • A further ~441 failures are the cascade (RuntimeError: no running event loop after the failed init).
  • Minimal repro (Windows, with --block-network in addopts): pytest lib/crewai/tests/test_flow_conversation.py -q — the first Flow construction fails before any test logic runs.

Expected behavior

CrewAI's own test suite should run on Windows with the repo's default socket-blocking addopts; emitting an event is not network access.

Suggested fix

Temporarily restore the pristine C-level socket.socket.connect only while the event-bus loop is constructed (event bus + file store), then put the guard back exactly as it was, so real network calls stay blocked. Happy to send that change with a regression test.

yuee3 avatar Oct 11 '26 07:10 yuee3

pytest-recording 0.14.1 (released today) allows socketpair() under --block-network (kiwicom/pytest-recording#213), so the event-loop failure is fixed upstream for that plugin. Once this lands, the guard added in #8051 should no longer be needed. Happy to help simplify it then.

simin-yuan avatar Nov 02 '26 18:11 simin-yuan