Skip to content

[Bug]: NullPointerException in WebSocketRouteImpl when page redirects during WebSocket message handling #1882

@nanne-rl

Description

@nanne-rl

Version

1.57.0

Steps to reproduce

TODO a test repo

Steps to reproduce:

  1. Set up a WebSocket route handler using page.routeWebSocket()
  2. Have the WebSocket receive a message that triggers a page navigation/redirect
  3. The redirect closes the page while the WebSocket handler is still processing

Expected behavior

The WebSocket connection should close gracefully when the page redirects, without throwing exceptions.

Actual behavior

Actual behavior:
A NullPointerException is thrown in WebSocketRouteImpl.java at int code = params.get("code").getAsInt(); because code and reason are null when the connection is closed due to page navigation.

Additional context

Analysis:
The JavaScript implementation doesn't send this data:
https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/dispatchers/webSocketRouteDispatcher.ts#L93

The Java implementation doesn't have null guards:
https://github.com/microsoft/playwright-java/blob/main/playwright/src/main/java/com/microsoft/playwright/impl/WebSocketRouteImpl.java#L163-L165

Suggested fix:
Add null checks or default values for code and reason in the close handler.

Environment

  • We use the docker image
  • OS: Linux
  • Java 21
  • All browsers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions