Skip to content

Commit

Permalink
fix: #1659 Telepathy LateUpdate processes a limited amount of message…
Browse files Browse the repository at this point in the history
…s per tick to avoid deadlocks (#1830)

* fix: #1659 Telepathy LateUpdate processes a limited amount of messages per tick to avoid deadlocks

* Update Assets/Mirror/Runtime/Transport/TelepathyTransport.cs

Co-authored-by: James Frowen <jamesfrowendev@gmail.com>

* Update Assets/Mirror/Runtime/Transport/TelepathyTransport.cs

Co-authored-by: James Frowen <jamesfrowendev@gmail.com>

Co-authored-by: James Frowen <jamesfrowendev@gmail.com>
  • Loading branch information
miwarnec and James-Frowen committed May 2, 2020
1 parent a6ab352 commit d3dccd7
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions Assets/Mirror/Runtime/Transport/TelepathyTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ public int MaxMessageSize
set => serverMaxMessageSize = clientMaxMessageSize = value;
}

[Header("Server")]
[Tooltip("Protect against allocation attacks by keeping the max message size small. Otherwise an attacker might send multiple fake packets with 2GB headers, causing the server to run out of memory after allocating multiple large packets.")]
[FormerlySerializedAs("MaxMessageSize")] public int serverMaxMessageSize = 16 * 1024;

[Tooltip("Server processes a limit amount of messages per tick to avoid a deadlock where it might end up processing forever if messages come in faster than we can process them.")]
public int serverMaxReceivesPerTick = 10000;

[Header("Client")]
[Tooltip("Protect against allocation attacks by keeping the max message size small. Otherwise an attacker host might send multiple fake packets with 2GB headers, causing the connected clients to run out of memory after allocating multiple large packets.")]
[FormerlySerializedAs("MaxMessageSize")] public int clientMaxMessageSize = 16 * 1024;

[Tooltip("Client processes a limit amount of messages per tick to avoid a deadlock where it might end up processing forever if messages come in faster than we can process them.")]
public int clientMaxReceivesPerTick = 1000;


protected Telepathy.Client client = new Telepathy.Client();
protected Telepathy.Server server = new Telepathy.Server();

Expand Down Expand Up @@ -117,8 +126,28 @@ public void LateUpdate()
// note: we need to check enabled in case we set it to false
// when LateUpdate already started.
// (https://github.com/vis2k/Mirror/pull/379)
while (enabled && ProcessClientMessage()) { }
while (enabled && ProcessServerMessage()) { }
if (!enabled)
return;

// process a maximum amount of client messages per tick
for (int i = 0; i < clientMaxReceivesPerTick; ++i)
{
// stop when there is no more message
if (!ProcessClientMessage())
{
break;
}
}

// process a maximum amount of server messages per tick
for (int i = 0; i < serverMaxReceivesPerTick; ++i)
{
// stop when there is no more message
if (!ProcessServerMessage())
{
break;
}
}
}

public override Uri ServerUri()
Expand Down

0 comments on commit d3dccd7

Please sign in to comment.