Skip to content

Commit 19e95e2

Browse files
committed
streams: Make libuv read/write callbacks manage python code exec sections
1 parent 8136414 commit 19e95e2

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

uvloop/handles/stream.pyx

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,9 @@ cdef void __uv_stream_on_shutdown(uv.uv_shutdown_t* req,
429429
return
430430

431431

432-
cdef void __uv_stream_on_read(uv.uv_stream_t* stream,
433-
ssize_t nread,
434-
const uv.uv_buf_t* buf) with gil:
435-
436-
# callback for uv_read_start
437-
438-
if __ensure_handle_data(<uv.uv_handle_t*>stream,
439-
"UVStream read callback") == 0:
440-
return
441-
432+
cdef inline void __uv_stream_on_read_impl(uv.uv_stream_t* stream,
433+
ssize_t nread,
434+
const uv.uv_buf_t* buf):
442435
cdef:
443436
UVStream sc = <UVStream>stream.data
444437
Loop loop = sc._loop
@@ -514,18 +507,7 @@ cdef void __uv_stream_on_read(uv.uv_stream_t* stream,
514507
sc._error(exc, False)
515508

516509

517-
cdef void __uv_stream_on_write(uv.uv_write_t* req, int status) with gil:
518-
# callback for uv_write
519-
520-
if req.data is NULL:
521-
# Shouldn't happen as:
522-
# - _StreamWriteContext does an extra INCREF in its 'init()'
523-
# - _StreamWriteContext holds a ref to the relevant UVStream
524-
aio_logger.error(
525-
'UVStream.write callback called with NULL req.data, status=%r',
526-
status)
527-
return
528-
510+
cdef inline void __uv_stream_on_write_impl(uv.uv_write_t* req, int status):
529511
cdef:
530512
_StreamWriteContext ctx = <_StreamWriteContext> req.data
531513
UVStream stream = <UVStream>ctx.stream
@@ -554,3 +536,44 @@ cdef void __uv_stream_on_write(uv.uv_write_t* req, int status) with gil:
554536
stream._loop._debug_stream_write_cb_errors_total += 1
555537

556538
stream._error(exc, False)
539+
540+
541+
cdef void __uv_stream_on_read(uv.uv_stream_t* stream,
542+
ssize_t nread,
543+
const uv.uv_buf_t* buf) with gil:
544+
545+
if __ensure_handle_data(<uv.uv_handle_t*>stream,
546+
"UVStream read callback") == 0:
547+
return
548+
549+
cdef:
550+
Loop loop = <Loop>stream.loop.data
551+
bint old_exec_py_code
552+
553+
old_exec_py_code = loop._executing_py_code
554+
loop._executing_py_code = 1
555+
# Don't need try-finally, __uv_stream_on_read_impl is void
556+
__uv_stream_on_read_impl(stream, nread, buf)
557+
loop._executing_py_code = old_exec_py_code
558+
559+
560+
cdef void __uv_stream_on_write(uv.uv_write_t* req, int status) with gil:
561+
562+
if req.data is NULL:
563+
# Shouldn't happen as:
564+
# - _StreamWriteContext does an extra INCREF in its 'init()'
565+
# - _StreamWriteContext holds a ref to the relevant UVStream
566+
aio_logger.error(
567+
'UVStream.write callback called with NULL req.data, status=%r',
568+
status)
569+
return
570+
571+
cdef:
572+
Loop loop = <UVStream>(<_StreamWriteContext> req.data).stream._loop
573+
bint old_exec_py_code
574+
575+
old_exec_py_code = loop._executing_py_code
576+
loop._executing_py_code = 1
577+
# Don't need try-finally, __uv_stream_on_write_impl is void
578+
__uv_stream_on_write_impl(req, status)
579+
loop._executing_py_code = old_exec_py_code

0 commit comments

Comments
 (0)