Skip to content

Commit b56788d

Browse files
committed
Revert "src: restore stdio on program exit"
This reverts commit c2c9c0c. It seems to be causing hangs when piping output to other processes.
1 parent f86a181 commit b56788d

File tree

1 file changed

+6
-94
lines changed

1 file changed

+6
-94
lines changed

src/node.cc

Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ typedef int mode_t;
100100
#else
101101
#include <pthread.h>
102102
#include <sys/resource.h> // getrlimit, setrlimit
103-
#include <termios.h> // tcgetattr, tcsetattr
104103
#include <unistd.h> // setuid, getuid
105104
#endif
106105

@@ -173,9 +172,6 @@ using v8::Value;
173172
static Mutex process_mutex;
174173
static Mutex environ_mutex;
175174

176-
// Safe to call more than once and from signal handlers.
177-
inline void PlatformExit();
178-
179175
static bool print_eval = false;
180176
static bool force_repl = false;
181177
static bool syntax_check_only = false;
@@ -886,7 +882,7 @@ void AppendExceptionLine(Environment* env,
886882
Mutex::ScopedLock lock(process_mutex);
887883
env->set_printed_error(true);
888884

889-
PlatformExit();
885+
uv_tty_reset_mode();
890886
PrintErrorString("\n%s", arrow);
891887
return;
892888
}
@@ -2797,7 +2793,7 @@ void SetupProcessObject(Environment* env,
27972793

27982794

27992795
void SignalExit(int signo) {
2800-
PlatformExit();
2796+
uv_tty_reset_mode();
28012797
v8_platform.StopTracingAgent();
28022798
#ifdef __FreeBSD__
28032799
// FreeBSD has a nasty bug, see RegisterSignalHandler for details
@@ -3629,27 +3625,6 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
36293625
}
36303626

36313627

3632-
#ifdef __POSIX__
3633-
static struct {
3634-
int flags;
3635-
bool isatty;
3636-
struct stat stat;
3637-
struct termios termios;
3638-
} stdio[1 + STDERR_FILENO];
3639-
3640-
3641-
inline int GetFileDescriptorFlags(int fd) {
3642-
int flags;
3643-
3644-
do {
3645-
flags = fcntl(fd, F_GETFL);
3646-
} while (flags == -1 && errno == EINTR);
3647-
3648-
return flags;
3649-
}
3650-
#endif // __POSIX__
3651-
3652-
36533628
inline void PlatformInit() {
36543629
#ifdef __POSIX__
36553630
#if HAVE_INSPECTOR
@@ -3660,18 +3635,16 @@ inline void PlatformInit() {
36603635
#endif // HAVE_INSPECTOR
36613636

36623637
// Make sure file descriptors 0-2 are valid before we start logging anything.
3663-
for (auto& s : stdio) {
3664-
const int fd = &s - stdio;
3665-
if (fstat(fd, &s.stat) == 0)
3638+
for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd += 1) {
3639+
struct stat ignored;
3640+
if (fstat(fd, &ignored) == 0)
36663641
continue;
36673642
// Anything but EBADF means something is seriously wrong. We don't
36683643
// have to special-case EINTR, fstat() is not interruptible.
36693644
if (errno != EBADF)
36703645
ABORT();
36713646
if (fd != open("/dev/null", O_RDWR))
36723647
ABORT();
3673-
if (fstat(fd, &s.stat) != 0)
3674-
ABORT();
36753648
}
36763649

36773650
#if HAVE_INSPECTOR
@@ -3694,24 +3667,6 @@ inline void PlatformInit() {
36943667
}
36953668
#endif // !NODE_SHARED_MODE
36963669

3697-
// Record the state of the stdio file descriptors so we can restore it
3698-
// on exit. Needs to happen before installing signal handlers because
3699-
// they make use of that information.
3700-
for (auto& s : stdio) {
3701-
const int fd = &s - stdio;
3702-
int err;
3703-
3704-
s.flags = GetFileDescriptorFlags(fd);
3705-
CHECK_NE(s.flags, -1);
3706-
3707-
if (!isatty(fd)) continue;
3708-
s.isatty = true;
3709-
do {
3710-
err = tcgetattr(fd, &s.termios);
3711-
} while (err == -1 && errno == EINTR);
3712-
CHECK_EQ(err, 0);
3713-
}
3714-
37153670
RegisterSignalHandler(SIGINT, SignalExit, true);
37163671
RegisterSignalHandler(SIGTERM, SignalExit, true);
37173672

@@ -3752,49 +3707,6 @@ inline void PlatformInit() {
37523707
}
37533708

37543709

3755-
// This function must be safe to call more than once and from signal handlers.
3756-
inline void PlatformExit() {
3757-
#ifdef __POSIX__
3758-
for (auto& s : stdio) {
3759-
const int fd = &s - stdio;
3760-
3761-
struct stat tmp;
3762-
if (-1 == fstat(fd, &tmp)) {
3763-
CHECK_EQ(errno, EBADF); // Program closed file descriptor.
3764-
continue;
3765-
}
3766-
3767-
bool is_same_file =
3768-
(s.stat.st_dev == tmp.st_dev && s.stat.st_ino == tmp.st_ino);
3769-
if (!is_same_file) continue; // Program reopened file descriptor.
3770-
3771-
int flags = GetFileDescriptorFlags(fd);
3772-
CHECK_NE(flags, -1);
3773-
3774-
// Restore the O_NONBLOCK flag if it changed.
3775-
if (O_NONBLOCK & (flags ^ s.flags)) {
3776-
flags &= ~O_NONBLOCK;
3777-
flags |= s.flags & O_NONBLOCK;
3778-
3779-
int err;
3780-
do {
3781-
err = fcntl(fd, F_SETFL, flags);
3782-
} while (err == -1 && errno == EINTR);
3783-
CHECK_NE(err, -1);
3784-
}
3785-
3786-
if (s.isatty) {
3787-
int err;
3788-
do {
3789-
err = tcsetattr(fd, TCSANOW, &s.termios);
3790-
} while (err == -1 && errno == EINTR);
3791-
CHECK_NE(err, -1);
3792-
}
3793-
}
3794-
#endif // __POSIX__
3795-
}
3796-
3797-
37983710
void ProcessArgv(int* argc,
37993711
const char** argv,
38003712
int* exec_argc,
@@ -4265,7 +4177,7 @@ inline int Start(uv_loop_t* event_loop,
42654177
}
42664178

42674179
int Start(int argc, char** argv) {
4268-
atexit([] () { PlatformExit(); });
4180+
atexit([] () { uv_tty_reset_mode(); });
42694181
PlatformInit();
42704182
performance::performance_node_start = PERFORMANCE_NOW();
42714183

0 commit comments

Comments
 (0)