Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocketConnetction.SendBytes([]) possible null reference when this.Socket is null #341

Open
NoNotNow opened this issue Jun 18, 2024 · 0 comments · May be fixed by #343
Open

WebSocketConnetction.SendBytes([]) possible null reference when this.Socket is null #341

NoNotNow opened this issue Jun 18, 2024 · 0 comments · May be fixed by #343

Comments

@NoNotNow
Copy link

During send operations from different threads we frequently get null reference exceptions.

Object reference not set to an instance of an object.
   at Fleck.WebSocketConnection.CloseSocket()
   at Fleck.WebSocketConnection.<>c__DisplayClass61_0.<SendBytes>b__1(Exception e)
   at Fleck.SocketWrapper.<>c__DisplayClass29_0.<Send>b__3(Task t)
   at System.Threading.Tasks.Task.Execute()

The stack trace points to this code in WebSocketConnection:

private Task SendBytes(byte[] bytes, Action callback = null)
  {
    return this.Socket.Send(bytes, (Action) (() =>
    {
      FleckLog.Debug("Sent " + (object) bytes.Length + " bytes");
      if (callback == null)
        return;
      callback();
    }), (Action<Exception>) (e =>
    {
      if (e is IOException)
        FleckLog.Debug("Failed to send. Disconnecting.", e);
      else
        FleckLog.Info("Failed to send. Disconnecting.", e);
      this.CloseSocket();
    }));
  }

I suggest adding null checks to the CloseSocket method

   private void CloseSocket()
    {
      this._closing = true;
      this.OnClose();
      this._closed = true;
      this.Socket?.Close();
      this.Socket?.Dispose();
      this._closing = false;
    }
@NoNotNow NoNotNow changed the title WebSocketConnetction.SendBytes possible null reference WebSocketConnetction.SendBytes([]) possible null reference when this.Socket is null Jun 19, 2024
@NoNotNow NoNotNow linked a pull request Jul 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant