-
Notifications
You must be signed in to change notification settings - Fork 672
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
Socket析构close(fd)返回-1 #37
Comments
close(fd)返回-1有以下几种情况,可以根据errno来判断: 绝大部分情况应该是EBADF,当你close(fd)的时候,它已经是一个失效的fd了。 epoll add error: bad file descriptor和close没有关系,close掉的fd不会被加入到epoll,可能是加入的fd已经被关闭、或者是一个无效fd。 |
没错,我看到的所有返回-1的情况都为EBADF (errno=9)。
我猜测这可能是某些调用已经关闭了该sockfd
另外,我是一个c++新手,想问问线程池类,关于add的定义中,添加tasks时是否正确?
代码中是workers_.emplace_back([task]() { (*task)(); });//这是线程的数组,我认为应该修改为
tasks_.emplace([task]() { (*task)(); });//这是将目标任务添加到任务队列中
可能我的理解方向不正确,上面是我近期学习注意到的。
…------------------ 原始邮件 ------------------
发件人: ***@***.***>;
发送时间: 2022年11月4日(星期五) 晚上11:20
收件人: ***@***.***>;
抄送: "what I ***@***.***>; ***@***.***>;
主题: Re: [yuesong-feng/30dayMakeCppServer] Socket析构close(fd)返回-1 (Issue #37)
close(fd)返回-1有以下几种情况,可以根据errno来判断:
EBADF:fd不是一个有效的、开启的文件描述符
EINTR:close()被一个信号中断了
EIO:发生了一个IO错误
ENOSPC, EDQUOT:在NFS文件系统中,第一次写入超过了可用的存储空间大小并不会发生,但后续的write()、fsync()或close()会发生。
绝大部分情况应该是EBADF,当你close(fd)的时候,它已经是一个失效的fd了。
这可能是fd在之前某个时候从epoll上被移除,因为从epoll上移除会自动调用close()来关闭文件描述符。
epoll add error: bad file descriptor和close没有关系,close掉的fd不会被加入到epoll,可能是加入的fd已经被关闭、或者是一个无效fd。
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我尝试打印出socket析构时候close(fd)的结果,发现它总是返回-1。
然后我在connection 读取缓冲区完毕后close(fd),这里就返回0正常关闭了套接字。
然后有时我还会遇到epoll add error: bad file descriptor的问题,这也许也和之前没有close有关吗?
The text was updated successfully, but these errors were encountered: