godotengine / godotengine/godot

WebRTCMultiplayerPeer reports as server when initialized with create_client

Open
#98,802 0 comments 0 reactions 0 assignees View on GitHub
bug topic:multiplayer
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

- Reproduced in v4.3.stable.official [77dcf97d8]

### System information

Godot v4.3.stable - macOS 15.0.0 - GLES3 (Compatibility) - Intel(R) Iris(TM) Plus Graphics OpenGL Engine - Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz (8 Threads)

### Issue description

**tldr**: When using `WebRTCMultiplayerPeer` initialized with `create_client`, the `multiplayer.is_server()` call can return true after the connection to the server is closed.

This seems to happen if a client-server connection is established, and then the server is disconnected. [`WebRTCMultiplayerPeer::get_unique_id()` will return 1, regardless of the network mode](https://github.com/godotengine/godot/blob/1bffd6c73b44b85e5889f54e14b2193940cf5bb1/modules/webrtc/webrtc_multiplayer_peer.cpp#L256). Before the disconnect, `multiplayer.is_server()` will return `false`. When a disconnect happens, it will start returning `true` ( [because it compares the result of `get_unique_id` to 1](https://github.com/godotengine/godot/blob/1bffd6c73b44b85e5889f54e14b2193940cf5bb1/scene/main/multiplayer_api.h#L76) )

Two issues with this are:
- this is undocumented
- there seems to be an inconsistency in behaviour with other multiplayer peers (e.g. [WebSocketMultiplayerPeer](https://github.com/godotengine/godot/blob/1bffd6c73b44b85e5889f54e14b2193940cf5bb1/modules/websocket/websocket_multiplayer_peer.cpp#L173) will never flip; [ENetMultiplayerPeer](https://github.com/godotengine/godot/blob/1bffd6c73b44b85e5889f54e14b2193940cf5bb1/modules/enet/enet_multiplayer_peer.cpp#L439) I think would flip in the opposite direction when inactive)

I spotted this because I was relying on `multiplayer.is_server()` to determine when to make some RPC calls, or which signals to subscribe to. There is an error message about the disconnect state, but it was not obvious that it would affect the network mode reporting.

While this seems like a bug to me, I don't know enough about Godot or WebRTC to be confident this needs a change. If you think this is not an issue, I'm happy to open a godot-docs PR to clarify this behaviour.

### Steps to reproduce

- Create a new multiplayer peer: `var rtc_mp = WebRTCMultiplayerPeer.new()`
- Initialize as a client: `rtc_mp.create_client(2)`
- Assign to multiplayer `multiplayer.multiplayer_peer = rtc_mp`
- `multiplayer.is_server()` will return `false` until the server disconnects
- Disconnect server
- Verify that `multiplayer.is_server()` returns `true` (but it should return `false`, because we initialized with `create_client`)

There is an attached MRP, which you can just run and wait a few seconds, and see the console output.

### Minimal reproduction project (MRP)

[webrtcrepro.zip](https://github.com/user-attachments/files/17612222/webrtcrepro.zip)

To run this locally you might need the [webrtc-native](https://github.com/godotengine/webrtc-native) extension. I'm not including it in the MRP cause it's ~300 mb with all the libs.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.