Closed serious-snow closed 2 months ago
I think the following code changes should fix this bug:
diff --git a/connection_unix.go b/connection_unix.go
index 68699f16..cfce3a92 100644
--- a/connection_unix.go
+++ b/connection_unix.go
@@ -325,13 +325,13 @@ func (c *conn) Next(n int) (buf []byte, err error) {
}
head, tail := c.inboundBuffer.Peek(n)
defer c.inboundBuffer.Discard(n) //nolint:errcheck
- if len(head) == n {
+ if len(head) >= n {
return head[:n], err
}
c.loop.cache.Reset()
c.loop.cache.Write(head)
c.loop.cache.Write(tail)
- if inBufferLen == n {
+ if inBufferLen >= n {
return c.loop.cache.Bytes(), err
}
@@ -352,13 +352,13 @@ func (c *conn) Peek(n int) (buf []byte, err error) {
return c.buffer[:n], err
}
head, tail := c.inboundBuffer.Peek(n)
- if len(head) == n {
+ if len(head) >= n {
return head[:n], err
}
c.loop.cache.Reset()
c.loop.cache.Write(head)
c.loop.cache.Write(tail)
- if inBufferLen == n {
+ if inBufferLen >= n {
return c.loop.cache.Bytes(), err
}
Could you verify if these code changes resolve this problem on your machines and return here? Thanks! If it does, you can open a pull request merging those code changes into gnet
if you're interested in contributing code to this project.
FYI, this bug should have been caused by these code changes.
I think the following code changes should fix this bug:我认为以下代码更改应该可以修复此错误:
diff --git a/connection_unix.go b/connection_unix.go index 68699f16..cfce3a92 100644 --- a/connection_unix.go +++ b/connection_unix.go @@ -325,13 +325,13 @@ func (c *conn) Next(n int) (buf []byte, err error) { } head, tail := c.inboundBuffer.Peek(n) defer c.inboundBuffer.Discard(n) //nolint:errcheck - if len(head) == n { + if len(head) >= n { return head[:n], err } c.loop.cache.Reset() c.loop.cache.Write(head) c.loop.cache.Write(tail) - if inBufferLen == n { + if inBufferLen >= n { return c.loop.cache.Bytes(), err } @@ -352,13 +352,13 @@ func (c *conn) Peek(n int) (buf []byte, err error) { return c.buffer[:n], err } head, tail := c.inboundBuffer.Peek(n) - if len(head) == n { + if len(head) >= n { return head[:n], err } c.loop.cache.Reset() c.loop.cache.Write(head) c.loop.cache.Write(tail) - if inBufferLen == n { + if inBufferLen >= n { return c.loop.cache.Bytes(), err }
Could you verify if these code changes resolve this problem on your machines and return here? Thanks! If it does, you can open a pull request merging those code changes into
gnet
if you're interested in contributing code to this project.您能否验证这些代码更改是否在您的计算机上解决了此问题并返回此处?谢谢!如果是这样,如果你有兴趣向这个项目贡献代码,可以打开一个拉取请求,将这些代码更改合并到其中gnet
。
it works, thanks. If I'm going to create a pr, do I need to write additional test code for this bug?
it works, thanks. If I'm going to create a pr, do I need to write additional test code for this bug?
It seems hard to write such a test that can reproduce this issue, but it would be nice if you could create one. But I'm OK to review a PR without that test, after all, it only reverts the code to the previous version.
it works, thanks. If I'm going to create a pr, do I need to write additional test code for this bug?
It seems hard to write such a test that can reproduce this issue, but it would be nice if you could create one. But I'm OK to review a PR without that test, after all, it only reverts the code to the previous version.
没有写测试用例,想到方法的时候再来这里补充吧。pr 在这里,https://github.com/panjf2000/gnet/pull/616, ps: 这是我第一次提交pr, 可能有什么不规范的地方。
Fixed by #616
Actions I've taken before I'm here
What happened?
gnet.Conn.Peek 发生崩溃 测试过 Next 一样
经过测试: 2.5.0+会出现这个问题,换到2.4.2正常
如果每次读取完数据也不会崩溃,但是目前我的需求就是想控制每个请求发包的数量,免得for循环占用太多时间,我想处理一定条数就关闭连接。
Major version of gnet
v2
Specific version of gnet
v2.5.0 +
Operating system
Linux, macOS
OS version
Linux 5.15.0-105-generic x86_64,Darwin 23.5.0 arm64
Go version
go version go1.22.0 darwin/arm64
Relevant log output
Code snippets (optional)
How to Reproduce
dnsperf -T 12 -c 12 -d records.txt -s 127.0.0.1 -l 60 -p 53 -m tcp
records.txt 内容
Does this issue reproduce with the latest release?
It can reproduce with the latest release