Skip to content

Commit 97d939a

Browse files
committed
src: store fd for libuv streams on Windows
On Windows, we can't just look up a FD for libuv streams and return it in `GetFD()`. However, we do sometimes construct streams from their FDs; in those cases, it should be okay to store the value on a class field. PR-URL: #19377 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c8fe8e8 commit 97d939a

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

src/pipe_wrap.cc

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
204204
int fd = args[0]->Int32Value();
205205

206206
int err = uv_pipe_open(&wrap->handle_, fd);
207+
wrap->set_fd(fd);
207208

208209
if (err != 0)
209210
env->isolate()->ThrowException(UVException(err, "uv_pipe_open"));

src/stream_wrap.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,14 @@ void LibuvStreamWrap::AddMethods(Environment* env,
115115

116116

117117
int LibuvStreamWrap::GetFD() {
118+
#ifdef _WIN32
119+
return fd_;
120+
#else
118121
int fd = -1;
119-
#if !defined(_WIN32)
120122
if (stream() != nullptr)
121123
uv_fileno(reinterpret_cast<uv_handle_t*>(stream()), &fd);
122-
#endif
123124
return fd;
125+
#endif
124126
}
125127

126128

src/stream_wrap.h

+18
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase {
8888
v8::Local<v8::FunctionTemplate> target,
8989
int flags = StreamBase::kFlagNone);
9090

91+
protected:
92+
inline void set_fd(int fd) {
93+
#ifdef _WIN32
94+
fd_ = fd;
95+
#endif
96+
}
97+
98+
9199
private:
92100
static void GetWriteQueueSize(
93101
const v8::FunctionCallbackInfo<v8::Value>& info);
@@ -101,6 +109,16 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase {
101109
static void AfterUvShutdown(uv_shutdown_t* req, int status);
102110

103111
uv_stream_t* const stream_;
112+
113+
#ifdef _WIN32
114+
// We don't always have an FD that we could look up on the stream_
115+
// object itself on Windows. However, for some cases, we open handles
116+
// using FDs; In that case, we can store and provide the value.
117+
// This became necessary because it allows to detect situations
118+
// where multiple handles refer to the same stdio FDs (in particular,
119+
// a possible IPC channel and a regular process.std??? stream).
120+
int fd_ = -1;
121+
#endif
104122
};
105123

106124

src/tcp_wrap.cc

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
212212
args.GetReturnValue().Set(UV_EBADF));
213213
int fd = static_cast<int>(args[0]->IntegerValue());
214214
uv_tcp_open(&wrap->handle_, fd);
215+
wrap->set_fd(fd);
215216
}
216217

217218

src/tty_wrap.cc

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ TTYWrap::TTYWrap(Environment* env,
172172
reinterpret_cast<uv_stream_t*>(&handle_),
173173
AsyncWrap::PROVIDER_TTYWRAP) {
174174
*init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
175+
set_fd(fd);
175176
if (*init_err != 0)
176177
MarkAsUninitialized();
177178
}

0 commit comments

Comments
 (0)