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

[Bug]: windows下的实现似乎有bug #509

Closed
3 tasks done
Zhanghailin1995 opened this issue Oct 19, 2023 · 2 comments
Closed
3 tasks done

[Bug]: windows下的实现似乎有bug #509

Zhanghailin1995 opened this issue Oct 19, 2023 · 2 comments
Assignees
Labels
bug Something isn't working waiting for response waiting for the response from commenter

Comments

@Zhanghailin1995
Copy link

Actions I've taken before I'm here

  • I've thoroughly read the documentations on this issue but still have no clue.
  • I've searched the Github Issues but didn't find any duplicate issues that have been resolved.
  • I've searched the internet for this issue but didn't find anything helpful.

What happened?

windows下使用gnet2.3.0时似乎有bug,使用gnet.Client.Dial返回一个conn时,直接调用conn.Wake, 会唤起OnTraffic,如果此时在OnTraffic方法中从conn中读取数据,立马就空指针崩溃了,因为此时windows下的实现的conn的buffer为nil,简单的复现代码如下。在linux环境下表现正常。至于为什么要立马调用Wake方法,是因为我在项目中想通过Wake的回调来设置Context,因为conn.Context和GetContext都不是并发安全的

Major version of gnet

v2

Specific version of gnet

v2.3.0

Operating system

Windows

OS version

10 22H2

Go version

go 1.17

Relevant log output

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x8 pc=0x584942]

goroutine 6 [running]:
github.com/valyala/bytebufferpool.(*ByteBuffer).Len(...)
        C:/Users/*****/go/pkg/mod/github.com/valyala/bytebufferpool@v1.0.0/byte
buffer.go:21
github.com/panjf2000/gnet/v2.(*conn).Peek(0xc00005be50, 0x425fc5)
        C:/Users/*****/go/pkg/mod/github.com/panjf2000/gnet/v2@v2.3.0/connectio
n_windows.go:164 +0x62
main.(*clientEvents).OnTraffic(0x0, {0x61efc0, 0xc0000244e0})
        C:/Workspace/Code/Go/go-sample/gnet_test/examples/client/main.go:19 +0x2
f
github.com/panjf2000/gnet/v2.(*eventloop).wake(0xc000024480, 0xc0000244e0)      
        C:/Users/*****/go/pkg/mod/github.com/panjf2000/gnet/v2@v2.3.0/eventloop
_windows.go:178 +0x5b
github.com/panjf2000/gnet/v2.(*conn).Wake.func2()
        C:/Users/*****/go/pkg/mod/github.com/panjf2000/gnet/v2@v2.3.0/connectio
n_windows.go:437 +0x7b
github.com/panjf2000/gnet/v2.(*eventloop).run(0xc000024480)
        C:/Users/*****/go/pkg/mod/github.com/panjf2000/gnet/v2@v2.3.0/eventloop
_windows.go:79 +0x1f3
golang.org/x/sync/errgroup.(*Group).Go.func1()
        C:/Users/*****/go/pkg/mod/golang.org/x/sync@v0.2.0/errgroup/errgroup.go
:75 +0x64
created by golang.org/x/sync/errgroup.(*Group).Go
        C:/Users/*****/go/pkg/mod/golang.org/x/sync@v0.2.0/errgroup/errgroup.go
:72 +0xa9

Code snippets (optional)

package main

import (
	"github.com/panjf2000/gnet/v2"
	"github.com/panjf2000/gnet/v2/pkg/logging"
	"log"
	"time"
)

type clientEvents struct {
	*gnet.BuiltinEventEngine
}

func (ev *clientEvents) OnBoot(e gnet.Engine) gnet.Action {
	return gnet.None
}

func (ev *clientEvents) OnTraffic(c gnet.Conn) (action gnet.Action) {
	_, err := c.Peek(10)
	if err != nil {
		log.Println(err)
	}
	return gnet.None
}

func main() {

	ev := &clientEvents{}
	client, _ := gnet.NewClient(ev, gnet.WithLogLevel(logging.DebugLevel),
		gnet.WithLockOSThread(true))
	client.Start()

	conn, _ := client.Dial("tcp", ":7399")
	conn.Wake(func(c gnet.Conn, err error) error {
		return nil
	})

	for {
		time.Sleep(time.Second * 10)
	}
}

How to Reproduce

Steps to reproduce the behavior:

  1. Go to '....'
  2. Click on '....'
  3. Do '....'
  4. See '....'

Does this issue reproduce with the latest release?

I haven't verified it with the latest release

@Zhanghailin1995 Zhanghailin1995 added the bug Something isn't working label Oct 19, 2023
@panjf2000
Copy link
Owner

如果只是要设置 context,在 OnOpen 里设置不就行了?

@panjf2000 panjf2000 added the waiting for response waiting for the response from commenter label Oct 19, 2023
panjf2000 added a commit that referenced this issue Oct 19, 2023
@panjf2000
Copy link
Owner

正如我前面说所,你的使用应该是有问题,不过考虑到这里确实存在代码在 Windows 和其他操作系统上行为不一致的问题,所以,还是要修复一下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

2 participants