Project

General

Profile

Bug #14968 » 0001-io.c-make-all-pipes-nonblocking-by-default.patch

normalperson (Eric Wong), 08/06/2018 04:46 AM

View differences:

io.c
return ret;
}
static int
rb_fd_set_nonblock(int fd)
{
#ifdef _WIN32
return rb_w32_set_nonblock(fd);
#elif defined(F_GETFL)
int oflags;
int err;
oflags = fcntl(fd, F_GETFL);
if (oflags == -1)
return -1;
oflags |= O_NONBLOCK;
err = fcntl(fd, F_SETFL, oflags);
if (err == -1)
return -1;
#endif
return 0;
}
int
rb_cloexec_pipe(int fildes[2])
{
......
#if defined(HAVE_PIPE2)
static int try_pipe2 = 1;
if (try_pipe2) {
ret = pipe2(fildes, O_CLOEXEC);
ret = pipe2(fildes, O_CLOEXEC | O_NONBLOCK);
if (ret != -1)
return ret;
/* pipe2 is available since Linux 2.6.27, glibc 2.9. */
......
#endif
rb_maygvl_fd_fix_cloexec(fildes[0]);
rb_maygvl_fd_fix_cloexec(fildes[1]);
rb_fd_set_nonblock(fildes[0]);
rb_fd_set_nonblock(fildes[1]);
return ret;
}
......
void
rb_io_set_nonblock(rb_io_t *fptr)
{
#ifdef _WIN32
if (rb_w32_set_nonblock(fptr->fd) != 0) {
if (rb_fd_set_nonblock(fptr->fd) != 0) {
rb_sys_fail_path(fptr->pathv);
}
#else
int oflags;
#ifdef F_GETFL
oflags = fcntl(fptr->fd, F_GETFL);
if (oflags == -1) {
rb_sys_fail_path(fptr->pathv);
}
#else
oflags = 0;
#endif
if ((oflags & O_NONBLOCK) == 0) {
oflags |= O_NONBLOCK;
if (fcntl(fptr->fd, F_SETFL, oflags) == -1) {
rb_sys_fail_path(fptr->pathv);
}
}
#endif
}
struct read_internal_arg {
process.c
rb_thread_stop_timer_thread();
}
#define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
#ifdef _WIN32
int rb_w32_set_nonblock2(int fd, int nonblock);
#endif
static int
set_blocking(int fd)
{
#ifdef _WIN32
return rb_w32_set_nonblock2(fd, 0);
#elif defined(F_GETFL) && defined(F_SETFL)
int fl = fcntl(fd, F_GETFL); /* async-signal-safe */
/* EBADF ought to be possible */
if (fl == -1) return fl;
if (fl & O_NONBLOCK) {
fl &= ~O_NONBLOCK;
return fcntl(fd, F_SETFL, fl);
}
return 0;
#endif
}
static void
stdfd_clear_nonblock(void)
{
/* many programs cannot deal with non-blocking stdin/stdout/stderr */
int fd;
for (fd = 0; fd < 3; fd++) {
if (set_blocking(fd) != 0) {
int e = errno;
if (e != EBADF) {
rb_async_bug_errno("set_blocking failed before exec", e);
}
}
}
}
static void
before_exec(void)
{
......
rb_execarg_allocate_dup2_tmpbuf(sargp, RARRAY_LEN(ary));
}
}
stdfd_clear_nonblock();
return 0;
}
......
{
ssize_t r;
if (set_blocking(fd) != 0) {
rb_async_bug_errno("set_blocking failed reading child error", errno);
}
do {
r = read(fd, buf, len);
} while (r < 0 && errno == EINTR);
spec/ruby/core/io/read_nonblock_spec.rb
platform_is_not :windows do
it 'sets the IO in nonblock mode' do
require 'io/nonblock'
@read.nonblock?.should == false
@write.write "abc"
@read.read_nonblock(1).should == "a"
@read.nonblock?.should == true
spec/ruby/core/io/write_nonblock_spec.rb
platform_is_not :windows do
it 'sets the IO in nonblock mode' do
require 'io/nonblock'
@write.nonblock?.should == false
@write.write_nonblock('a')
@write.nonblock?.should == true
end
test/io/nonblock/test_flush.rb
def test_nonblock
IO.pipe {|r, w|
w.nonblock = false
assert_equal(false, w.nonblock?)
w.nonblock do
assert_equal(true, w.nonblock?)
test/ruby/test_io.rb
def test_readpartial_lock
with_pipe do |r, w|
s = ""
r.nonblock = false
t = Thread.new { r.readpartial(5, s) }
Thread.pass until t.stop?
assert_raise(RuntimeError) { s.clear }
test/ruby/test_process.rb
Process.wait pid
end
}
# ensure standard FDs we redirect to are blocking for compatibility
with_pipes(4) do |pipes|
src = 'p [STDIN,STDOUT,STDERR].map(&:nonblock?)'
rdr = { 0 => pipes[0][0], 1 => pipes[1][1], 2 => pipes[2][1] }
pid = spawn(RUBY, '-rio/nonblock', '-e', src, rdr)
assert_equal("[false, false, false]\n", pipes[1][0].gets)
end
end
end
thread.c
struct timespec ts, end, *tsp;
const struct timespec *to;
int drained;
rb_thread_t *th = GET_THREAD();
nfds_t nfds;
rb_unblock_function_t *ubf;
struct waiting_fd wfd;
enum ruby_tag_type state;
wfd.th = GET_THREAD();
wfd.fd = fd;
timeout_prepare(&tsp, &ts, &end, timeout);
fds[0].fd = fd;
fds[0].events = (short)events;
do {
fds[0].revents = 0;
fds[1].fd = rb_sigwait_fd_get(th);
fds[1].fd = rb_sigwait_fd_get(wfd.th);
if (fds[1].fd >= 0) {
fds[1].events = POLLIN;
......
}
lerrno = 0;
BLOCKING_REGION(th, {
to = sigwait_timeout(th, fds[1].fd, tsp, &drained);
result = ppoll(fds, nfds, to, NULL);
if (result < 0) lerrno = errno;
}, ubf, th, FALSE);
list_add(&wfd.th->vm->waiting_fds, &wfd.wfd_node);
EC_PUSH_TAG(wfd.th->ec);
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
BLOCKING_REGION(wfd.th, {
to = sigwait_timeout(wfd.th, fds[1].fd, tsp, &drained);
result = ppoll(fds, nfds, to, NULL);
if (result < 0) lerrno = errno;
}, ubf, wfd.th, FALSE);
}
EC_POP_TAG();
list_del(&wfd.wfd_node);
if (fds[1].fd >= 0) {
if (result > 0 && fds[1].revents) {
result--;
fds[1].revents = 0;
}
(void)check_signals_nogvl(th, fds[1].fd);
rb_sigwait_fd_put(th, fds[1].fd);
rb_sigwait_fd_migrate(th->vm);
(void)check_signals_nogvl(wfd.th, fds[1].fd);
rb_sigwait_fd_put(wfd.th, fds[1].fd);
rb_sigwait_fd_migrate(wfd.th->vm);
}
if (state) {
EC_JUMP_TAG(wfd.th->ec, state);
}
RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
} while (wait_retryable(&result, lerrno, tsp, &end));
if (result < 0) {
......
rb_fdset_t *read;
rb_fdset_t *write;
rb_fdset_t *except;
struct waiting_fd wfd;
struct timeval *tv;
};
......
{
struct select_args *args = (struct select_args *)ptr;
list_del(&args->wfd.wfd_node);
if (args->read) rb_fd_term(args->read);
if (args->write) rb_fd_term(args->write);
if (args->except) rb_fd_term(args->except);
......
args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
args.tv = tv;
args.wfd.fd = fd;
args.wfd.th = GET_THREAD();
list_add(&args.wfd.th->vm->waiting_fds, &args.wfd.wfd_node);
r = (int)rb_ensure(select_single, ptr, select_single_cleanup, ptr);
if (r == -1)
errno = args.as.error;
win32/win32.c
/* License: Ruby's */
int
rb_w32_set_nonblock(int fd)
rb_w32_set_nonblock2(int fd, int nonblock)
{
SOCKET sock = TO_SOCKET(fd);
if (is_socket(sock)) {
return setfl(sock, O_NONBLOCK);
return setfl(sock, nonblock ? O_NONBLOCK : 0);
}
else if (is_pipe(sock)) {
DWORD state;
......
errno = map_errno(GetLastError());
return -1;
}
state |= PIPE_NOWAIT;
if (nonblock) {
state |= PIPE_NOWAIT;
}
else {
state &= ~PIPE_NOWAIT;
}
if (!SetNamedPipeHandleState((HANDLE)sock, &state, NULL, NULL)) {
errno = map_errno(GetLastError());
return -1;
......
}
}
int
rb_w32_set_nonblock(int fd)
{
return rb_w32_set_nonblock2(fd, TRUE);
}
#ifndef WNOHANG
#define WNOHANG -1
#endif
-
(1-1/2)