valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.87k stars 1.76k forks source link

Unpredictable behaviour in `fasthttp.hijackConn` after closure #1630

Closed askolesov closed 1 year ago

askolesov commented 1 year ago

Currently, the way fasthttp.hijackConn behaves is different from what can be expected with the net.Conn interface.

Normally, when you close a net.Conn, if you try to read or write again, you get errors.

But with fasthttp.hijackConn, when you close it, it gets cleaned and put back in the pool. If you try to read from or write to it again, it can cause two problems:

  1. If it hasn't been reused yet, it might cause null reference exception because the underlying connection is nil.
  2. If it has been reused, you might end up reading from or writing to a completely different connection, which is very bad.

I suggest not reusing hijackConn when KeepHijackedConns is enabled (it still will be reused when this option is disabled). This might make things a bit slower, but it will make the behavior more predictable for users.

We've had issues with this in our project, and it seems like the easiest way to fix them is with this pull request.

askolesov commented 1 year ago

Here is the fix: https://github.com/valyala/fasthttp/pull/1629