stream模块当sock:receive还未超时,如何能快速关闭连接?

100 views
Skip to first unread message

luke

unread,
Jan 9, 2018, 1:25:32 AM1/9/18
to openresty
Hi~

如题。

假设sock:receive超时设置很大,然后tcp已建立连接,

然后又在外部调用nginx -s quit, 想在判断ngx.worker.exiting()为true时清理掉连接,

此时子协程的sock:receive还未返回 ,但是在父协程中直接调用 sock:close() 报错,

似乎是sock立即被gc掉了。何解?

luke

unread,
Jan 9, 2018, 2:00:34 AM1/9/18
to openresty
ngx.exit(0) 可以立即退出,但是是强退,lua 子线程并不是正常退掉。

在 2018年1月9日星期二 UTC+8下午2:25:32,luke写道:

tokers

unread,
Jan 9, 2018, 2:11:16 AM1/9/18
to openresty
> 假设sock:receive超时设置很大,然后tcp已建立连接,

> 然后又在外部调用nginx -s quit, 想在判断ngx.worker.exiting()为true时清理掉连接,

> 此时子协程的sock:receive还未返回 ,但是在父协程中直接调用 sock:close() 报错,

> 似乎是sock立即被gc掉了。何解?

报的错误应该是类似于 socket busy reading 吧?

因为这个时候这个 cosocket 对象正在 receive 数据,是不允许其他的协程去操作它的(write 除外,因为是全双工的)。

luke

unread,
Jan 9, 2018, 2:15:34 AM1/9/18
to openresty
不是,在handle中调用sock:close() 报的是如下错误:

2018/01/09 14:17:34 [error] 9772#8956: *1 lua entry thread aborted: runtime error:
...test-confirm\network\lua\openresty-test\logic\stream.lua:130:
attempt to call method 'close' (a nil value)



在 2018年1月9日星期二 UTC+8下午3:11:16,tokers写道:

tokers

unread,
Jan 9, 2018, 2:27:54 AM1/9/18
to openresty
Hi!

从这个错误信息来看是你用的这个 cosocket 对象不包含 close 函数。

对 stream 子系统不太熟悉 :)

不知道是不是和 http 子系统类似,也有着不同类型的 cosocket,比如 http 子系统有 req socket,这个 socket 只有 receive receiveuntil settimeout 和 setimeouts 这四个接口;类似的还有 raw req socket。

可能你需要查询下对应的文档,看看你使用的 cosocket 类型对应有哪些接口可用。

~~ 仅供参考。
Reply all
Reply to author
Forward
0 new messages