sta / sta/websocket-sharp

Receiving a large amount of frames in a short period causes StackOverflowException.

Open
#550 2 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

The overflow is caused by the recursive call of receive() in WebSocket.startReceiving().

private void startReceiving()
{
	if (_messageEventQueue.Count > 0)
		_messageEventQueue.Clear();

	_pongReceived = new ManualResetEvent(false);
	_receivingExited = new ManualResetEvent(false);

	Action receive = null;
	receive =
	  () =>
		WebSocketFrame.ReadFrameAsync(
		  _stream,
		  false,
		  frame =>
		  {
			  if (!processReceivedFrame(frame) || _readyState == WebSocketState.Closed)
			  {
				  var exited = _receivingExited;
				  if (exited != null)
					  exited.Set();

				  return;
			  }

			  // Receive next asap because the Ping or Close needs a response to it.
			  receive();

			  if (_inMessage || !HasMessage || _readyState != WebSocketState.Open)
				  return;

			  message();
		  },
		  ex =>
		  {
			  _logger.Fatal(ex.ToString());
			  fatal("An exception has occurred while receiving.", ex);
		  }
		);

	receive();
}

Any ideas on how to get rid of the recursion?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at WebSocket.startReceiving and WebSocketFrame.ReadFrameAsync, then trace how the callback schedules the next receive while processing a burst of frames. Reproduce the large-frame-rate case and verify that receiving completes without StackOverflowException while preserving Ping and Close handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.