Skip to content

Flaky test: closes_on_malformed_json fails when Ping frame arrives before Close frame #278

@leynos

Description

@leynos

Description

The test backend::inbound::ws::session::tests::closes_on_malformed_json intermittently fails in CI with:

thread 'inbound::ws::session::tests::closes_on_malformed_json' panicked at backend/src/inbound/ws/session_tests.rs:136:18: expected close frame, got Ping(b"")

Root Cause

The test assumes the next frame after sending malformed JSON will be a Close frame. However, the WebSocket session has a heartbeat mechanism that sends Ping frames at regular intervals. When timing aligns unfavorably, a Ping frame can arrive before the Close frame, causing the assertion to fail.

Current Code (lines 131-137)

  let frame = socket.next().await.expect("response frame").expect("frame");
  match frame {
      Frame::Close(reason) => {
          assert_eq!(reason.expect("reason").code, CloseCode::Policy);
      }
      other => panic!("expected close frame, got {other:?}"),
  }

Suggested Fix

Follow the pattern used in closes_after_timeout_without_client_messages which correctly handles intermediate Ping/Pong frames by looping until a Close frame is received:

  let frame = loop {
      let frame = socket.next().await.expect("response frame").expect("frame");
      match frame {
          Frame::Ping(_) | Frame::Pong(_) => continue,
          other => break other,
      }
  };

  match frame {
      Frame::Close(reason) => {
          assert_eq!(reason.expect("reason").code, CloseCode::Policy);
      }
      other => panic!("expected close frame, got {other:?}"),
  }

Reproduction

The failure is timing-dependent and occurs sporadically. It was observed during CI runs for PR #277 but passes on local rerun.

Location

  • File: backend/src/inbound/ws/session_tests.rs:122-138
  • Test: closes_on_malformed_json

Labels

bug, flaky-test, backend

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions