From: "byroot (Jean Boussier)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109961] [Ruby master Bug#19012] BasicSocket#recv* methods return an empty packet instead of nil on closed connections
Date: Tue, 20 Sep 2022 13:52:41 +0000 (UTC) [thread overview]
Message-ID: <redmine.issue-19012.20220920135241.7941@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19012.20220920135241.7941@ruby-lang.org
Issue #19012 has been reported by byroot (Jean Boussier).
----------------------------------------
Bug #19012: BasicSocket#recv* methods return an empty packet instead of nil on closed connections
https://bugs.ruby-lang.org/issues/19012
* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
`man recvmsg(2)` states:
> Return Value
> These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.
But somehow the entire `receiv` family of methods in Ruby seem to interpret `0` as empty string instead of "EOF".
```ruby
require 'socket'
puts "=== pipes ==="
r, w = IO.pipe
r.read_nonblock(1, exception: false) # => :wait_readable
w.close
r.read_nonblock(1, exception: false) # => nil (EOF)
puts "=== sockets ===="
r, w = UNIXSocket.socketpair
r.read_nonblock(1, exception: false) # => :wait_readable
r.recvmsg_nonblock(1, exception: false) # => :wait_readable
r.recv_nonblock(1, exception: false) # => :wait_readable
w.close
r.read_nonblock(1, exception: false) # => nil (EOF)
r.recvmsg_nonblock(1, exception: false) # => ["", #<Addrinfo: empty-sockaddr SOCK_STREAM>, 128]]
r.recvmsg # => ["", #<Addrinfo: empty-sockaddr SOCK_STREAM>, 0]]
r.recv_nonblock(1, exception: false) # => ""
```
### Expected behavior
I would expect `recvmsg_nonblock`, `recvmsg`, `recv_nonblock` and `recv` to return `nil` when the connection is closed.
--
https://bugs.ruby-lang.org/
next parent reply other threads:[~2022-09-20 13:52 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-20 13:52 byroot (Jean Boussier) [this message]
2022-09-20 14:13 ` [ruby-core:109962] [Ruby master Bug#19012] BasicSocket#recv* methods return an empty packet instead of nil on closed connections byroot (Jean Boussier)
2022-09-21 8:02 ` [ruby-core:109972] " byroot (Jean Boussier)
2022-12-02 2:15 ` [ruby-core:111139] " mame (Yusuke Endoh)
2022-12-02 11:46 ` [ruby-core:111152] " byroot (Jean Boussier)
2022-12-02 11:52 ` [ruby-core:111153] " byroot (Jean Boussier)
2022-12-07 7:56 ` [ruby-core:111227] " byroot (Jean Boussier)
2023-12-17 19:52 ` [ruby-core:115768] " zverok (Victor Shepelev) via ruby-core
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.issue-19012.20220920135241.7941@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).