Skip to content

Commit d8fe153

Browse files
committed
Upgrade to libuv v1.26.0.
That required to change how WriteUnixTransport monitors the pipe for an EOF event. Prior to libuv v1.23.1 it was possible to use uv_read_start for that purpose. Now we're using a throw away uv_poll_t to monitor for UV_DISCONNECT.
1 parent fddc43a commit d8fe153

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

uvloop/handles/pipe.pxd

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ cdef class ReadUnixTransport(UVStream):
2727

2828
cdef class WriteUnixTransport(UVStream):
2929

30+
cdef:
31+
uv.uv_poll_t disconnect_listener
32+
bint disconnect_listener_inited
33+
3034
@staticmethod
3135
cdef WriteUnixTransport new(Loop loop, object protocol, Server server,
3236
object waiter)

uvloop/handles/pipe.pyx

+63
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ cdef class ReadUnixTransport(UVStream):
147147
@cython.no_gc_clear
148148
cdef class WriteUnixTransport(UVStream):
149149

150+
def __cinit__(self):
151+
self.disconnect_listener_inited = False
152+
self.disconnect_listener.data = NULL
153+
150154
@staticmethod
151155
cdef WriteUnixTransport new(Loop loop, object protocol, Server server,
152156
object waiter):
@@ -163,6 +167,46 @@ cdef class WriteUnixTransport(UVStream):
163167
__pipe_init_uv_handle(<UVStream>handle, loop)
164168
return handle
165169

170+
cdef _start_reading(self):
171+
# A custom implementation for monitoring for EOF:
172+
# libuv since v1.23.1 prohibits using uv_read_start on
173+
# write-only FDs, so we use a throw-away uv_poll_t handle
174+
# for that purpose, as suggested in
175+
# https://github.com/libuv/libuv/issues/2058.
176+
177+
cdef int err
178+
179+
if not self.disconnect_listener_inited:
180+
err = uv.uv_poll_init(self._loop.uvloop,
181+
&self.disconnect_listener,
182+
self._fileno())
183+
if err < 0:
184+
raise convert_error(err)
185+
self.disconnect_listener.data = <void*>self
186+
self.disconnect_listener_inited = True
187+
188+
err = uv.uv_poll_start(&self.disconnect_listener,
189+
uv.UV_READABLE | uv.UV_DISCONNECT,
190+
__on_write_pipe_poll_event)
191+
if err < 0:
192+
raise convert_error(err)
193+
194+
cdef _stop_reading(self):
195+
cdef int err
196+
if not self.disconnect_listener_inited:
197+
return
198+
err = uv.uv_poll_stop(&self.disconnect_listener)
199+
if err < 0:
200+
raise convert_error(err)
201+
202+
cdef _close(self):
203+
if self.disconnect_listener_inited:
204+
self.disconnect_listener.data = NULL
205+
uv.uv_close(<uv.uv_handle_t *>(&self.disconnect_listener), NULL)
206+
self.disconnect_listener_inited = False
207+
208+
UVStream._close(self)
209+
166210
cdef _new_socket(self):
167211
return __pipe_get_socket(<UVSocketHandle>self)
168212

@@ -176,6 +220,25 @@ cdef class WriteUnixTransport(UVStream):
176220
raise NotImplementedError
177221

178222

223+
cdef void __on_write_pipe_poll_event(uv.uv_poll_t* handle,
224+
int status, int events) with gil:
225+
cdef WriteUnixTransport tr
226+
227+
if handle.data is NULL:
228+
return
229+
230+
tr = <WriteUnixTransport>handle.data
231+
if tr._closed:
232+
return
233+
234+
if events & uv.UV_DISCONNECT:
235+
try:
236+
tr._stop_reading()
237+
tr._on_eof()
238+
except BaseException as ex:
239+
tr._fatal_error(ex, False)
240+
241+
179242
cdef class _PipeConnectRequest(UVRequest):
180243
cdef:
181244
UnixTransport transport

uvloop/includes/compat.h

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
#include "uv.h"
44

55

6-
// uv_poll_event.UV_DISCONNECT is available since libuv v1.9.0
7-
#if UV_VERSION_HEX < 0x10900
8-
#define UV_DISCONNECT 0
9-
#endif
10-
116
#ifndef EWOULDBLOCK
127
#define EWOULDBLOCK EAGAIN
138
#endif

uvloop/includes/uv.pxd

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ from posix.types cimport gid_t, uid_t
44
from . cimport system
55

66

7-
cdef extern from "includes/compat.h":
8-
# Member of uv_poll_event, in compat.h for compatibility
9-
# with libuv < v1.9.0
10-
cdef int UV_DISCONNECT
11-
12-
137
cdef extern from "uv.h" nogil:
148
cdef int UV_TCP_IPV6ONLY
159

@@ -190,7 +184,7 @@ cdef extern from "uv.h" nogil:
190184
ctypedef enum uv_poll_event:
191185
UV_READABLE = 1,
192186
UV_WRITABLE = 2
193-
# UV_DISCONNECT = 4 ; see compat.h for details
187+
UV_DISCONNECT = 4
194188

195189
ctypedef enum uv_membership:
196190
UV_LEAVE_GROUP = 0,

vendor/libuv

Submodule libuv updated 133 files

0 commit comments

Comments
 (0)