Reopen the file descriptor when it refers to a tty. This lets us put the
tty in non-blocking mode without affecting other processes that share it
with us.
This brings back commit 31f9fbc, which was reverted in 20bb1bf. The OSX
select trick is working now.
Original patch by @bnoordhuis
Useful to know when the the event loop is empty, this can't be done with
uv_run() without possibly blocking, or running some events (which might
empty the event loop as a side-effect).
Set the close-on-exec flag on file descriptors that we've received with
recvmsg() so we don't leak them when calling fork() afterwards.
On Linux, we use the MSG_CMSG_CLOEXEC flag when supported (2.6.23 and
up.)
On older Linux versions and other platforms, we walk the received file
descriptors and set the close-on-exec flag for each fd manually. That
won't entirely avoid race conditions when other threads call fork() or
clone() but at least we're less likely to leak file descriptors now.
When fd is closed and new one (with the same number) is opened inside
kqueue/epoll/port loop's callback - stale events might invoke callbacks
on wrong watchers.
Check if watcher was changed after invocation and invalidate all events
with the same fd.
fix#826
On some systems, clock_gettime(CLOCK_MONOTONIC) is only serviced from
the vDSO when the __vdso_clock_gettime() wrapper is confident enough
that the vDSO timestamp is highly accurate. When in doubt, it falls
back to making a traditional SYS_clock_gettime system call with all
the overhead that entails.
While a commendable approach, it's overkill for our purposes because we
don't usually need high precision time. That's why this commit switches
to CLOCK_MONOTONIC_COARSE for low-precision timekeeping, provided said
clock has at least a one millisecond resolution.
This change should eliminate the system call on almost all systems,
including virtualized ones, provided the kernel is >= 2.6.32 and glibc
is new enough to find and parse the vDSO.
Ensure that close() system calls don't close stdio file descriptors
because that is almost never the intention.
This is also a partial workaround for a kernel bug that seems to affect
all Linux kernels when stdin is a pipe that gets closed: fd 0 keeps
signalling EPOLLHUP but a subsequent call to epoll_ctl(EPOLL_CTL_DEL)
fails with EBADF. See joyent/node#6271 for details and a test case.
This commit changes the libuv API to return error codes directly rather
than storing them in a loop-global field.
A code snippet like this one:
if (uv_foo(loop) < 0) {
uv_err_t err = uv_last_error(loop);
fprintf(stderr, "%s\n", uv_strerror(err));
}
Should be rewritten like this:
int err = uv_foo(loop);
if (err < 0)
fprintf(stderr, "%s\n", uv_strerror(err));
The rationale for this change is that it should make creating bindings
for other languages a lot easier: dealing with struct return values is
painful with most FFIs and often downright buggy.
Before this commit, creating an event loop, starting a timer and
calling uv_run(UV_RUN_ONCE) blocked in uv_run() until the timer
expired - but didn't actually run the timer.
Fix a buglet where uv_read_stop() would mark the handle as stopped even
when there are in-progress write requests.
This bug is unlikely to have affected anyone, the only case where it
has a user-visible effect is when:
a) the handle has been stopped for reading but not writing, and
b) it's the last active handle in the event loop's pollset
If both conditions are met, it's possible for the event loop to
terminate prematurely.
This reapplies commit 80f2f82 which was temporarily reverted in fe7b154
because it was making a lot of node.js tests fail on OS X with the
following assertion:
Assertion failed: (!uv__is_active(handle)), function
uv__finish_close, file ../../deps/uv/src/unix/core.c, line 165.
Expecting that the handle is inactive when the state is UV_CLOSING
turns out to be a bad assumption: it's possible that the handle is
executing (for example) a shutdown request when uv__finish_close()
is called. That's okay, uv__stream_destroy() takes care of that.
The issue wasn't specific to OS X, it was just more visible on that
platform. (Slow) debug builds on Linux exhibited the same behavior.
Fix the following two warnings:
src/unix/core.c:74:1: warning: ISO C90 forbids array
'static_assert_failed' whose size can't be evaluated [-Wvla]
src/unix/core.c:76:1: warning: ISO C90 forbids array
'static_assert_failed' whose size can't be evaluated [-Wvla]
* Make uv_stop() work when libuv is embedded in another event loop.
* Fix a small bug where loop->stop_flag was not reset when mode ==
UV_RUN_ONCE or UV_RUN_NOWAIT. The next call to uv_run() would return
immediately without doing any work.
Don't add the io watcher to the watcher queue if the requested change
is effectively a no-op, that is, when the event mask doesn't change.
The exception here is sunos because the event ports backend requires
that watched file descriptors are re-added on every turn of the event
loop.
This commit is a micro-optimization, it does not change the event
loop's observable behavior in any way.
This changes the prototype of uv_run() from:
int uv_run(uv_loop_t* loop);
To:
int uv_run(uv_loop_t* loop, uv_run_mode mode);
Where `mode` is UV_RUN_DEFAULT, UV_RUN_ONCE or UV_RUN_NOWAIT.
Fixes#683.
`#if FOO` (where FOO is undefined) is a legal construct in C89 and C99
but gcc, clang and sparse complain loudly about it at higher warning
levels.
Squelch those warnings. Makes the code more consistent as well.
Fixes a bug where timers expire prematurely when the following
conditions hold:
a) libuv first spends some time blocked in the platform poll function
b) a callback then calls uv_timer_start()
Cause: uv_timer_start() uses an out-of-date loop->time in its
'when should the timer callback run?' calculations.
Solution: Update loop->time before invoking any callbacks.
Fixes#678.
This commit renames the various uv_hrtime() implementations to uv__hrtime().
Libuv uses the high-res timer internally in performance-critical code paths.
Calling the non-public version avoids going through the PLT when libuv is
compiled as a shared object.
The exported uv_hrtime() now has a single definition in src/unix/core.c that
calls uv__hrtime().
A future optimization is to lift the uv__hrtime() declarations into header
files so they can be inlined at the call sites. Then again, linking with -flto
should accomplish the same thing.
Allows for running the event loop in 3 modes:
* default: loop runs until the refcount drops to zero
* once: poll for events only once and block until one is handled
* nowait: poll for events only once but don't block if there are
no pending events
Fix a rather obscure bug where the event loop stalls when an I/O watcher is
stopped while an artificial event, generated with uv__io_feed(), is pending.
This can be used in conjuction with uv_run_once() to poll in one thread and run
the event loop's event callbacks in another.
Useful for embedding libuv's event loop in another event loop.