Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

3425 linhas
116 KiB

  1. # cython: language_level=3, embedsignature=True
  2. import asyncio
  3. cimport cython
  4. from .includes.debug cimport UVLOOP_DEBUG
  5. from .includes cimport uv
  6. from .includes cimport system
  7. from .includes.python cimport (
  8. PY_VERSION_HEX,
  9. PyMem_RawMalloc, PyMem_RawFree,
  10. PyMem_RawCalloc, PyMem_RawRealloc,
  11. PyUnicode_EncodeFSDefault,
  12. PyErr_SetInterrupt,
  13. _Py_RestoreSignals,
  14. Context_CopyCurrent,
  15. Context_Enter,
  16. Context_Exit,
  17. PyMemoryView_FromMemory, PyBUF_WRITE,
  18. PyMemoryView_FromObject, PyMemoryView_Check,
  19. PyOS_AfterFork_Parent, PyOS_AfterFork_Child,
  20. PyOS_BeforeFork,
  21. PyUnicode_FromString
  22. )
  23. from .includes.flowcontrol cimport add_flowcontrol_defaults
  24. from libc.stdint cimport uint64_t
  25. from libc.string cimport memset, strerror, memcpy
  26. from libc cimport errno
  27. from cpython cimport PyObject
  28. from cpython cimport PyErr_CheckSignals, PyErr_Occurred
  29. from cpython cimport PyThread_get_thread_ident
  30. from cpython cimport Py_INCREF, Py_DECREF, Py_XDECREF, Py_XINCREF
  31. from cpython cimport (
  32. PyObject_GetBuffer, PyBuffer_Release, PyBUF_SIMPLE,
  33. Py_buffer, PyBytes_AsString, PyBytes_CheckExact,
  34. PyBytes_AsStringAndSize,
  35. Py_SIZE, PyBytes_AS_STRING, PyBUF_WRITABLE
  36. )
  37. from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer
  38. from . import _noop
  39. include "includes/stdlib.pxi"
  40. include "errors.pyx"
  41. cdef:
  42. int PY39 = PY_VERSION_HEX >= 0x03090000
  43. int PY311 = PY_VERSION_HEX >= 0x030b0000
  44. int PY313 = PY_VERSION_HEX >= 0x030d0000
  45. uint64_t MAX_SLEEP = 3600 * 24 * 365 * 100
  46. cdef _is_sock_stream(sock_type):
  47. if SOCK_NONBLOCK == -1:
  48. return sock_type == uv.SOCK_STREAM
  49. else:
  50. # Linux's socket.type is a bitmask that can include extra info
  51. # about socket (like SOCK_NONBLOCK bit), therefore we can't do simple
  52. # `sock_type == socket.SOCK_STREAM`, see
  53. # https://github.com/torvalds/linux/blob/v4.13/include/linux/net.h#L77
  54. # for more details.
  55. return (sock_type & 0xF) == uv.SOCK_STREAM
  56. cdef _is_sock_dgram(sock_type):
  57. if SOCK_NONBLOCK == -1:
  58. return sock_type == uv.SOCK_DGRAM
  59. else:
  60. # Read the comment in `_is_sock_stream`.
  61. return (sock_type & 0xF) == uv.SOCK_DGRAM
  62. cdef isfuture(obj):
  63. if aio_isfuture is None:
  64. return isinstance(obj, aio_Future)
  65. else:
  66. return aio_isfuture(obj)
  67. cdef inline socket_inc_io_ref(sock):
  68. if isinstance(sock, socket_socket):
  69. sock._io_refs += 1
  70. cdef inline socket_dec_io_ref(sock):
  71. if isinstance(sock, socket_socket):
  72. sock._decref_socketios()
  73. cdef inline run_in_context(context, method):
  74. # This method is internally used to workaround a reference issue that in
  75. # certain circumstances, inlined context.run() will not hold a reference to
  76. # the given method instance, which - if deallocated - will cause segfault.
  77. # See also: edgedb/edgedb#2222
  78. Py_INCREF(method)
  79. try:
  80. return context.run(method)
  81. finally:
  82. Py_DECREF(method)
  83. cdef inline run_in_context1(context, method, arg):
  84. Py_INCREF(method)
  85. try:
  86. return context.run(method, arg)
  87. finally:
  88. Py_DECREF(method)
  89. cdef inline run_in_context2(context, method, arg1, arg2):
  90. Py_INCREF(method)
  91. try:
  92. return context.run(method, arg1, arg2)
  93. finally:
  94. Py_DECREF(method)
  95. # Used for deprecation and removal of `loop.create_datagram_endpoint()`'s
  96. # *reuse_address* parameter
  97. _unset = object()
  98. @cython.no_gc_clear
  99. cdef class Loop:
  100. def __cinit__(self):
  101. cdef int err
  102. # Install PyMem* memory allocators if they aren't installed yet.
  103. __install_pymem()
  104. # Install pthread_atfork handlers
  105. __install_atfork()
  106. self.uvloop = <uv.uv_loop_t*>PyMem_RawMalloc(sizeof(uv.uv_loop_t))
  107. if self.uvloop is NULL:
  108. raise MemoryError()
  109. self.slow_callback_duration = 0.1
  110. self._closed = 0
  111. self._debug = 0
  112. self._thread_id = 0
  113. self._running = 0
  114. self._stopping = 0
  115. self._transports = weakref_WeakValueDictionary()
  116. self._processes = set()
  117. # Used to keep a reference (and hence keep the fileobj alive)
  118. # for as long as its registered by add_reader or add_writer.
  119. # This is how the selector module and hence asyncio behaves.
  120. self._fd_to_reader_fileobj = {}
  121. self._fd_to_writer_fileobj = {}
  122. self._unix_server_sockets = {}
  123. self._timers = set()
  124. self._polls = {}
  125. self._recv_buffer_in_use = 0
  126. err = uv.uv_loop_init(self.uvloop)
  127. if err < 0:
  128. raise convert_error(err)
  129. self.uvloop.data = <void*> self
  130. self._init_debug_fields()
  131. self.active_process_handler = None
  132. self._last_error = None
  133. self._task_factory = None
  134. self._exception_handler = None
  135. self._default_executor = None
  136. self._queued_streams = set()
  137. self._executing_streams = set()
  138. self._ready = col_deque()
  139. self._ready_len = 0
  140. self.handler_async = UVAsync.new(
  141. self, <method_t>self._on_wake, self)
  142. self.handler_idle = UVIdle.new(
  143. self,
  144. new_MethodHandle(
  145. self, "loop._on_idle", <method_t>self._on_idle, None, self))
  146. # Needed to call `UVStream._exec_write` for writes scheduled
  147. # during `Protocol.data_received`.
  148. self.handler_check__exec_writes = UVCheck.new(
  149. self,
  150. new_MethodHandle(
  151. self, "loop._exec_queued_writes",
  152. <method_t>self._exec_queued_writes, None, self))
  153. self._signals = set()
  154. self._ssock = self._csock = None
  155. self._signal_handlers = {}
  156. self._listening_signals = False
  157. self._old_signal_wakeup_id = -1
  158. self._coroutine_debug_set = False
  159. # A weak set of all asynchronous generators that are
  160. # being iterated by the loop.
  161. self._asyncgens = weakref_WeakSet()
  162. # Set to True when `loop.shutdown_asyncgens` is called.
  163. self._asyncgens_shutdown_called = False
  164. # Set to True when `loop.shutdown_default_executor` is called.
  165. self._executor_shutdown_called = False
  166. self._servers = set()
  167. cdef inline _is_main_thread(self):
  168. cdef uint64_t main_thread_id = system.MAIN_THREAD_ID
  169. if system.MAIN_THREAD_ID_SET == 0:
  170. main_thread_id = <uint64_t>threading_main_thread().ident
  171. system.setMainThreadID(main_thread_id)
  172. return main_thread_id == PyThread_get_thread_ident()
  173. def __init__(self):
  174. self.set_debug(
  175. sys_dev_mode or (not sys_ignore_environment
  176. and bool(os_environ.get('PYTHONASYNCIODEBUG'))))
  177. def __dealloc__(self):
  178. if self._running == 1:
  179. raise RuntimeError('deallocating a running event loop!')
  180. if self._closed == 0:
  181. aio_logger.error("deallocating an open event loop")
  182. return
  183. PyMem_RawFree(self.uvloop)
  184. self.uvloop = NULL
  185. cdef _init_debug_fields(self):
  186. self._debug_cc = bool(UVLOOP_DEBUG)
  187. if UVLOOP_DEBUG:
  188. self._debug_handles_current = col_Counter()
  189. self._debug_handles_closed = col_Counter()
  190. self._debug_handles_total = col_Counter()
  191. else:
  192. self._debug_handles_current = None
  193. self._debug_handles_closed = None
  194. self._debug_handles_total = None
  195. self._debug_uv_handles_total = 0
  196. self._debug_uv_handles_freed = 0
  197. self._debug_stream_read_cb_total = 0
  198. self._debug_stream_read_eof_total = 0
  199. self._debug_stream_read_errors_total = 0
  200. self._debug_stream_read_cb_errors_total = 0
  201. self._debug_stream_read_eof_cb_errors_total = 0
  202. self._debug_stream_shutdown_errors_total = 0
  203. self._debug_stream_listen_errors_total = 0
  204. self._debug_stream_write_tries = 0
  205. self._debug_stream_write_errors_total = 0
  206. self._debug_stream_write_ctx_total = 0
  207. self._debug_stream_write_ctx_cnt = 0
  208. self._debug_stream_write_cb_errors_total = 0
  209. self._debug_cb_handles_total = 0
  210. self._debug_cb_handles_count = 0
  211. self._debug_cb_timer_handles_total = 0
  212. self._debug_cb_timer_handles_count = 0
  213. self._poll_read_events_total = 0
  214. self._poll_read_cb_errors_total = 0
  215. self._poll_write_events_total = 0
  216. self._poll_write_cb_errors_total = 0
  217. self._sock_try_write_total = 0
  218. self._debug_exception_handler_cnt = 0
  219. cdef _setup_or_resume_signals(self):
  220. if not self._is_main_thread():
  221. return
  222. if self._listening_signals:
  223. raise RuntimeError('signals handling has been already setup')
  224. if self._ssock is not None:
  225. raise RuntimeError('self-pipe exists before loop run')
  226. # Create a self-pipe and call set_signal_wakeup_fd() with one
  227. # of its ends. This is needed so that libuv knows that it needs
  228. # to wakeup on ^C (no matter if the SIGINT handler is still the
  229. # standard Python's one or or user set their own.)
  230. self._ssock, self._csock = socket_socketpair()
  231. try:
  232. self._ssock.setblocking(False)
  233. self._csock.setblocking(False)
  234. fileno = self._csock.fileno()
  235. self._old_signal_wakeup_id = _set_signal_wakeup_fd(fileno)
  236. except Exception:
  237. # Out of all statements in the try block, only the
  238. # "_set_signal_wakeup_fd()" call can fail, but it shouldn't,
  239. # as we ensure that the current thread is the main thread.
  240. # Still, if something goes horribly wrong we want to clean up
  241. # the socket pair.
  242. self._ssock.close()
  243. self._csock.close()
  244. self._ssock = None
  245. self._csock = None
  246. raise
  247. self._add_reader(
  248. self._ssock,
  249. new_MethodHandle(
  250. self,
  251. "Loop._read_from_self",
  252. <method_t>self._read_from_self,
  253. None,
  254. self))
  255. self._listening_signals = True
  256. cdef _pause_signals(self):
  257. if not self._is_main_thread():
  258. if self._listening_signals:
  259. raise RuntimeError(
  260. 'cannot pause signals handling; no longer running in '
  261. 'the main thread')
  262. else:
  263. return
  264. if not self._listening_signals:
  265. raise RuntimeError('signals handling has not been setup')
  266. self._listening_signals = False
  267. _set_signal_wakeup_fd(self._old_signal_wakeup_id)
  268. self._remove_reader(self._ssock)
  269. self._ssock.close()
  270. self._csock.close()
  271. self._ssock = None
  272. self._csock = None
  273. cdef _shutdown_signals(self):
  274. if not self._is_main_thread():
  275. if self._signal_handlers:
  276. aio_logger.warning(
  277. 'cannot cleanup signal handlers: closing the event loop '
  278. 'in a non-main OS thread')
  279. return
  280. if self._listening_signals:
  281. raise RuntimeError(
  282. 'cannot shutdown signals handling as it has not been paused')
  283. if self._ssock:
  284. raise RuntimeError(
  285. 'self-pipe was not cleaned up after loop was run')
  286. for sig in list(self._signal_handlers):
  287. self.remove_signal_handler(sig)
  288. def __sighandler(self, signum, frame):
  289. self._signals.add(signum)
  290. cdef inline _ceval_process_signals(self):
  291. # Invoke CPython eval loop to let process signals.
  292. PyErr_CheckSignals()
  293. # Calling a pure-Python function will invoke
  294. # _PyEval_EvalFrameDefault which will process
  295. # pending signal callbacks.
  296. _noop.noop() # Might raise ^C
  297. cdef _read_from_self(self):
  298. cdef bytes sigdata
  299. sigdata = b''
  300. while True:
  301. try:
  302. data = self._ssock.recv(65536)
  303. if not data:
  304. break
  305. sigdata += data
  306. except InterruptedError:
  307. continue
  308. except BlockingIOError:
  309. break
  310. if sigdata:
  311. self._invoke_signals(sigdata)
  312. cdef _invoke_signals(self, bytes data):
  313. cdef set sigs
  314. self._ceval_process_signals()
  315. sigs = self._signals.copy()
  316. self._signals.clear()
  317. for signum in data:
  318. if not signum:
  319. # ignore null bytes written by set_wakeup_fd()
  320. continue
  321. sigs.discard(signum)
  322. self._handle_signal(signum)
  323. for signum in sigs:
  324. # Since not all signals are registered by add_signal_handler()
  325. # (for instance, we use the default SIGINT handler) not all
  326. # signals will trigger loop.__sighandler() callback. Therefore
  327. # we combine two datasources: one is self-pipe, one is data
  328. # from __sighandler; this ensures that signals shouldn't be
  329. # lost even if set_wakeup_fd() couldn't write to the self-pipe.
  330. self._handle_signal(signum)
  331. cdef _handle_signal(self, sig):
  332. cdef Handle handle
  333. try:
  334. handle = <Handle>(self._signal_handlers[sig])
  335. except KeyError:
  336. handle = None
  337. if handle is None:
  338. self._ceval_process_signals()
  339. return
  340. if handle._cancelled:
  341. self.remove_signal_handler(sig) # Remove it properly.
  342. else:
  343. self._append_ready_handle(handle)
  344. self.handler_async.send()
  345. cdef _on_wake(self):
  346. if ((self._ready_len > 0 or self._stopping) and
  347. not self.handler_idle.running):
  348. self.handler_idle.start()
  349. cdef _on_idle(self):
  350. cdef:
  351. int i, ntodo
  352. object popleft = self._ready.popleft
  353. Handle handler
  354. ntodo = len(self._ready)
  355. if self._debug:
  356. for i from 0 <= i < ntodo:
  357. handler = <Handle> popleft()
  358. if handler._cancelled == 0:
  359. try:
  360. started = time_monotonic()
  361. handler._run()
  362. except BaseException as ex:
  363. self._stop(ex)
  364. return
  365. else:
  366. delta = time_monotonic() - started
  367. if delta > self.slow_callback_duration:
  368. aio_logger.warning(
  369. 'Executing %s took %.3f seconds',
  370. handler._format_handle(), delta)
  371. else:
  372. for i from 0 <= i < ntodo:
  373. handler = <Handle> popleft()
  374. if handler._cancelled == 0:
  375. try:
  376. handler._run()
  377. except BaseException as ex:
  378. self._stop(ex)
  379. return
  380. if len(self._queued_streams):
  381. self._exec_queued_writes()
  382. self._ready_len = len(self._ready)
  383. if self._ready_len == 0 and self.handler_idle.running:
  384. self.handler_idle.stop()
  385. if self._stopping:
  386. uv.uv_stop(self.uvloop) # void
  387. cdef _stop(self, exc):
  388. if exc is not None:
  389. self._last_error = exc
  390. if self._stopping == 1:
  391. return
  392. self._stopping = 1
  393. if not self.handler_idle.running:
  394. self.handler_idle.start()
  395. cdef __run(self, uv.uv_run_mode mode):
  396. # Although every UVHandle holds a reference to the loop,
  397. # we want to do everything to ensure that the loop will
  398. # never deallocate during the run -- so we do some
  399. # manual refs management.
  400. Py_INCREF(self)
  401. with nogil:
  402. err = uv.uv_run(self.uvloop, mode)
  403. Py_DECREF(self)
  404. if err < 0:
  405. raise convert_error(err)
  406. cdef _run(self, uv.uv_run_mode mode):
  407. cdef int err
  408. if self._closed == 1:
  409. raise RuntimeError('unable to start the loop; it was closed')
  410. if self._running == 1:
  411. raise RuntimeError('this event loop is already running.')
  412. if (aio_get_running_loop is not None and
  413. aio_get_running_loop() is not None):
  414. raise RuntimeError(
  415. 'Cannot run the event loop while another loop is running')
  416. # reset _last_error
  417. self._last_error = None
  418. self._thread_id = PyThread_get_thread_ident()
  419. self._running = 1
  420. self.handler_check__exec_writes.start()
  421. self.handler_idle.start()
  422. self._setup_or_resume_signals()
  423. if aio_set_running_loop is not None:
  424. aio_set_running_loop(self)
  425. try:
  426. self.__run(mode)
  427. finally:
  428. if aio_set_running_loop is not None:
  429. aio_set_running_loop(None)
  430. self.handler_check__exec_writes.stop()
  431. self.handler_idle.stop()
  432. self._pause_signals()
  433. self._thread_id = 0
  434. self._running = 0
  435. self._stopping = 0
  436. if self._last_error is not None:
  437. # The loop was stopped with an error with 'loop._stop(error)' call
  438. raise self._last_error
  439. cdef _close(self):
  440. cdef int err
  441. if self._running == 1:
  442. raise RuntimeError("Cannot close a running event loop")
  443. if self._closed == 1:
  444. return
  445. self._closed = 1
  446. for cb_handle in self._ready:
  447. cb_handle.cancel()
  448. self._ready.clear()
  449. self._ready_len = 0
  450. if self._polls:
  451. for poll_handle in self._polls.values():
  452. (<UVHandle>poll_handle)._close()
  453. self._polls.clear()
  454. if self._timers:
  455. for timer_cbhandle in tuple(self._timers):
  456. timer_cbhandle.cancel()
  457. # Close all remaining handles
  458. self.handler_async._close()
  459. self.handler_idle._close()
  460. self.handler_check__exec_writes._close()
  461. __close_all_handles(self)
  462. self._shutdown_signals()
  463. # During this run there should be no open handles,
  464. # so it should finish right away
  465. self.__run(uv.UV_RUN_DEFAULT)
  466. if self._fd_to_writer_fileobj:
  467. for fileobj in self._fd_to_writer_fileobj.values():
  468. socket_dec_io_ref(fileobj)
  469. self._fd_to_writer_fileobj.clear()
  470. if self._fd_to_reader_fileobj:
  471. for fileobj in self._fd_to_reader_fileobj.values():
  472. socket_dec_io_ref(fileobj)
  473. self._fd_to_reader_fileobj.clear()
  474. if self._timers:
  475. raise RuntimeError(
  476. f"new timers were queued during loop closing: {self._timers}")
  477. if self._polls:
  478. raise RuntimeError(
  479. f"new poll handles were queued during loop closing: "
  480. f"{self._polls}")
  481. if self._ready:
  482. raise RuntimeError(
  483. f"new callbacks were queued during loop closing: "
  484. f"{self._ready}")
  485. err = uv.uv_loop_close(self.uvloop)
  486. if err < 0:
  487. raise convert_error(err)
  488. self.handler_async = None
  489. self.handler_idle = None
  490. self.handler_check__exec_writes = None
  491. self._executor_shutdown_called = True
  492. executor = self._default_executor
  493. if executor is not None:
  494. self._default_executor = None
  495. executor.shutdown(wait=False)
  496. cdef uint64_t _time(self):
  497. # asyncio doesn't have a time cache, neither should uvloop.
  498. uv.uv_update_time(self.uvloop) # void
  499. return uv.uv_now(self.uvloop)
  500. cdef inline _queue_write(self, UVStream stream):
  501. self._queued_streams.add(stream)
  502. if not self.handler_check__exec_writes.running:
  503. self.handler_check__exec_writes.start()
  504. cdef _exec_queued_writes(self):
  505. if len(self._queued_streams) == 0:
  506. if self.handler_check__exec_writes.running:
  507. self.handler_check__exec_writes.stop()
  508. return
  509. cdef:
  510. UVStream stream
  511. streams = self._queued_streams
  512. self._queued_streams = self._executing_streams
  513. self._executing_streams = streams
  514. try:
  515. for pystream in streams:
  516. stream = <UVStream>pystream
  517. stream._exec_write()
  518. finally:
  519. streams.clear()
  520. if self.handler_check__exec_writes.running:
  521. if len(self._queued_streams) == 0:
  522. self.handler_check__exec_writes.stop()
  523. cdef inline _call_soon(self, object callback, object args, object context):
  524. cdef Handle handle
  525. handle = new_Handle(self, callback, args, context)
  526. self._call_soon_handle(handle)
  527. return handle
  528. cdef inline _append_ready_handle(self, Handle handle):
  529. self._check_closed()
  530. self._ready.append(handle)
  531. self._ready_len += 1
  532. cdef inline _call_soon_handle(self, Handle handle):
  533. self._append_ready_handle(handle)
  534. if not self.handler_idle.running:
  535. self.handler_idle.start()
  536. cdef _call_later(self, uint64_t delay, object callback, object args,
  537. object context):
  538. return TimerHandle(self, callback, args, delay, context)
  539. cdef void _handle_exception(self, object ex):
  540. if isinstance(ex, Exception):
  541. self.call_exception_handler({'exception': ex})
  542. else:
  543. # BaseException
  544. self._last_error = ex
  545. # Exit ASAP
  546. self._stop(None)
  547. cdef inline _check_signal(self, sig):
  548. if not isinstance(sig, int):
  549. raise TypeError('sig must be an int, not {!r}'.format(sig))
  550. if not (1 <= sig < signal_NSIG):
  551. raise ValueError(
  552. 'sig {} out of range(1, {})'.format(sig, signal_NSIG))
  553. cdef inline _check_closed(self):
  554. if self._closed == 1:
  555. raise RuntimeError('Event loop is closed')
  556. cdef inline _check_thread(self):
  557. if self._thread_id == 0:
  558. return
  559. cdef uint64_t thread_id
  560. thread_id = <uint64_t>PyThread_get_thread_ident()
  561. if thread_id != self._thread_id:
  562. raise RuntimeError(
  563. "Non-thread-safe operation invoked on an event loop other "
  564. "than the current one")
  565. cdef inline _new_future(self):
  566. return aio_Future(loop=self)
  567. cdef _track_transport(self, UVBaseTransport transport):
  568. self._transports[transport._fileno()] = transport
  569. cdef _track_process(self, UVProcess proc):
  570. self._processes.add(proc)
  571. cdef _untrack_process(self, UVProcess proc):
  572. self._processes.discard(proc)
  573. cdef _fileobj_to_fd(self, fileobj):
  574. """Return a file descriptor from a file object.
  575. Parameters:
  576. fileobj -- file object or file descriptor
  577. Returns:
  578. corresponding file descriptor
  579. Raises:
  580. ValueError if the object is invalid
  581. """
  582. # Copy of the `selectors._fileobj_to_fd()` function.
  583. if isinstance(fileobj, int):
  584. fd = fileobj
  585. else:
  586. try:
  587. fd = int(fileobj.fileno())
  588. except (AttributeError, TypeError, ValueError):
  589. raise ValueError("Invalid file object: "
  590. "{!r}".format(fileobj)) from None
  591. if fd < 0:
  592. raise ValueError("Invalid file descriptor: {}".format(fd))
  593. return fd
  594. cdef _ensure_fd_no_transport(self, fd):
  595. cdef UVBaseTransport tr
  596. try:
  597. tr = <UVBaseTransport>(self._transports[fd])
  598. except KeyError:
  599. pass
  600. else:
  601. if tr._is_alive():
  602. raise RuntimeError(
  603. 'File descriptor {!r} is used by transport {!r}'.format(
  604. fd, tr))
  605. cdef _add_reader(self, fileobj, Handle handle):
  606. cdef:
  607. UVPoll poll
  608. self._check_closed()
  609. fd = self._fileobj_to_fd(fileobj)
  610. self._ensure_fd_no_transport(fd)
  611. try:
  612. poll = <UVPoll>(self._polls[fd])
  613. except KeyError:
  614. poll = UVPoll.new(self, fd)
  615. self._polls[fd] = poll
  616. poll.start_reading(handle)
  617. old_fileobj = self._fd_to_reader_fileobj.pop(fd, None)
  618. if old_fileobj is not None:
  619. socket_dec_io_ref(old_fileobj)
  620. self._fd_to_reader_fileobj[fd] = fileobj
  621. socket_inc_io_ref(fileobj)
  622. cdef _remove_reader(self, fileobj):
  623. cdef:
  624. UVPoll poll
  625. fd = self._fileobj_to_fd(fileobj)
  626. self._ensure_fd_no_transport(fd)
  627. mapped_fileobj = self._fd_to_reader_fileobj.pop(fd, None)
  628. if mapped_fileobj is not None:
  629. socket_dec_io_ref(mapped_fileobj)
  630. if self._closed == 1:
  631. return False
  632. try:
  633. poll = <UVPoll>(self._polls[fd])
  634. except KeyError:
  635. return False
  636. result = poll.stop_reading()
  637. if not poll.is_active():
  638. del self._polls[fd]
  639. poll._close()
  640. return result
  641. cdef _has_reader(self, fileobj):
  642. cdef:
  643. UVPoll poll
  644. self._check_closed()
  645. fd = self._fileobj_to_fd(fileobj)
  646. try:
  647. poll = <UVPoll>(self._polls[fd])
  648. except KeyError:
  649. return False
  650. return poll.is_reading()
  651. cdef _add_writer(self, fileobj, Handle handle):
  652. cdef:
  653. UVPoll poll
  654. self._check_closed()
  655. fd = self._fileobj_to_fd(fileobj)
  656. self._ensure_fd_no_transport(fd)
  657. try:
  658. poll = <UVPoll>(self._polls[fd])
  659. except KeyError:
  660. poll = UVPoll.new(self, fd)
  661. self._polls[fd] = poll
  662. poll.start_writing(handle)
  663. old_fileobj = self._fd_to_writer_fileobj.pop(fd, None)
  664. if old_fileobj is not None:
  665. socket_dec_io_ref(old_fileobj)
  666. self._fd_to_writer_fileobj[fd] = fileobj
  667. socket_inc_io_ref(fileobj)
  668. cdef _remove_writer(self, fileobj):
  669. cdef:
  670. UVPoll poll
  671. fd = self._fileobj_to_fd(fileobj)
  672. self._ensure_fd_no_transport(fd)
  673. mapped_fileobj = self._fd_to_writer_fileobj.pop(fd, None)
  674. if mapped_fileobj is not None:
  675. socket_dec_io_ref(mapped_fileobj)
  676. if self._closed == 1:
  677. return False
  678. try:
  679. poll = <UVPoll>(self._polls[fd])
  680. except KeyError:
  681. return False
  682. result = poll.stop_writing()
  683. if not poll.is_active():
  684. del self._polls[fd]
  685. poll._close()
  686. return result
  687. cdef _has_writer(self, fileobj):
  688. cdef:
  689. UVPoll poll
  690. self._check_closed()
  691. fd = self._fileobj_to_fd(fileobj)
  692. try:
  693. poll = <UVPoll>(self._polls[fd])
  694. except KeyError:
  695. return False
  696. return poll.is_writing()
  697. cdef _getaddrinfo(self, object host, object port,
  698. int family, int type,
  699. int proto, int flags,
  700. int unpack):
  701. if isinstance(port, str):
  702. port = port.encode()
  703. elif isinstance(port, int):
  704. port = str(port).encode()
  705. if port is not None and not isinstance(port, bytes):
  706. raise TypeError('port must be a str, bytes or int')
  707. if isinstance(host, str):
  708. host = host.encode('idna')
  709. if host is not None:
  710. if not isinstance(host, bytes):
  711. raise TypeError('host must be a str or bytes')
  712. fut = self._new_future()
  713. def callback(result):
  714. if AddrInfo.isinstance(result):
  715. try:
  716. if unpack == 0:
  717. data = result
  718. else:
  719. data = (<AddrInfo>result).unpack()
  720. except (KeyboardInterrupt, SystemExit):
  721. raise
  722. except BaseException as ex:
  723. if not fut.cancelled():
  724. fut.set_exception(ex)
  725. else:
  726. if not fut.cancelled():
  727. fut.set_result(data)
  728. else:
  729. if not fut.cancelled():
  730. fut.set_exception(result)
  731. AddrInfoRequest(self, host, port, family, type, proto, flags, callback)
  732. return fut
  733. cdef _getnameinfo(self, system.sockaddr *addr, int flags):
  734. cdef NameInfoRequest nr
  735. fut = self._new_future()
  736. def callback(result):
  737. if isinstance(result, tuple):
  738. fut.set_result(result)
  739. else:
  740. fut.set_exception(result)
  741. nr = NameInfoRequest(self, callback)
  742. nr.query(addr, flags)
  743. return fut
  744. cdef _sock_recv(self, fut, sock, n):
  745. if UVLOOP_DEBUG:
  746. if fut.cancelled():
  747. # Shouldn't happen with _SyncSocketReaderFuture.
  748. raise RuntimeError(
  749. f'_sock_recv is called on a cancelled Future')
  750. if not self._has_reader(sock):
  751. raise RuntimeError(
  752. f'socket {sock!r} does not have a reader '
  753. f'in the _sock_recv callback')
  754. try:
  755. data = sock.recv(n)
  756. except (BlockingIOError, InterruptedError):
  757. # No need to re-add the reader, let's just wait until
  758. # the poll handler calls this callback again.
  759. pass
  760. except (KeyboardInterrupt, SystemExit):
  761. raise
  762. except BaseException as exc:
  763. fut.set_exception(exc)
  764. self._remove_reader(sock)
  765. else:
  766. fut.set_result(data)
  767. self._remove_reader(sock)
  768. cdef _sock_recv_into(self, fut, sock, buf):
  769. if UVLOOP_DEBUG:
  770. if fut.cancelled():
  771. # Shouldn't happen with _SyncSocketReaderFuture.
  772. raise RuntimeError(
  773. f'_sock_recv_into is called on a cancelled Future')
  774. if not self._has_reader(sock):
  775. raise RuntimeError(
  776. f'socket {sock!r} does not have a reader '
  777. f'in the _sock_recv_into callback')
  778. try:
  779. data = sock.recv_into(buf)
  780. except (BlockingIOError, InterruptedError):
  781. # No need to re-add the reader, let's just wait until
  782. # the poll handler calls this callback again.
  783. pass
  784. except (KeyboardInterrupt, SystemExit):
  785. raise
  786. except BaseException as exc:
  787. fut.set_exception(exc)
  788. self._remove_reader(sock)
  789. else:
  790. fut.set_result(data)
  791. self._remove_reader(sock)
  792. cdef _sock_sendall(self, fut, sock, data):
  793. cdef:
  794. Handle handle
  795. int n
  796. if UVLOOP_DEBUG:
  797. if fut.cancelled():
  798. # Shouldn't happen with _SyncSocketWriterFuture.
  799. raise RuntimeError(
  800. f'_sock_sendall is called on a cancelled Future')
  801. if not self._has_writer(sock):
  802. raise RuntimeError(
  803. f'socket {sock!r} does not have a writer '
  804. f'in the _sock_sendall callback')
  805. try:
  806. n = sock.send(data)
  807. except (BlockingIOError, InterruptedError):
  808. # Try next time.
  809. return
  810. except (KeyboardInterrupt, SystemExit):
  811. raise
  812. except BaseException as exc:
  813. fut.set_exception(exc)
  814. self._remove_writer(sock)
  815. return
  816. self._remove_writer(sock)
  817. if n == len(data):
  818. fut.set_result(None)
  819. else:
  820. if n:
  821. if not isinstance(data, memoryview):
  822. data = memoryview(data)
  823. data = data[n:]
  824. handle = new_MethodHandle3(
  825. self,
  826. "Loop._sock_sendall",
  827. <method3_t>self._sock_sendall,
  828. None,
  829. self,
  830. fut, sock, data)
  831. self._add_writer(sock, handle)
  832. cdef _sock_accept(self, fut, sock):
  833. try:
  834. conn, address = sock.accept()
  835. conn.setblocking(False)
  836. except (BlockingIOError, InterruptedError):
  837. # There is an active reader for _sock_accept, so
  838. # do nothing, it will be called again.
  839. pass
  840. except (KeyboardInterrupt, SystemExit):
  841. raise
  842. except BaseException as exc:
  843. fut.set_exception(exc)
  844. self._remove_reader(sock)
  845. else:
  846. fut.set_result((conn, address))
  847. self._remove_reader(sock)
  848. cdef _sock_connect(self, sock, address):
  849. cdef:
  850. Handle handle
  851. try:
  852. sock.connect(address)
  853. except (BlockingIOError, InterruptedError):
  854. pass
  855. else:
  856. return
  857. fut = _SyncSocketWriterFuture(sock, self)
  858. handle = new_MethodHandle3(
  859. self,
  860. "Loop._sock_connect",
  861. <method3_t>self._sock_connect_cb,
  862. None,
  863. self,
  864. fut, sock, address)
  865. self._add_writer(sock, handle)
  866. return fut
  867. cdef _sock_connect_cb(self, fut, sock, address):
  868. if UVLOOP_DEBUG:
  869. if fut.cancelled():
  870. # Shouldn't happen with _SyncSocketWriterFuture.
  871. raise RuntimeError(
  872. f'_sock_connect_cb is called on a cancelled Future')
  873. if not self._has_writer(sock):
  874. raise RuntimeError(
  875. f'socket {sock!r} does not have a writer '
  876. f'in the _sock_connect_cb callback')
  877. try:
  878. err = sock.getsockopt(uv.SOL_SOCKET, uv.SO_ERROR)
  879. if err != 0:
  880. # Jump to any except clause below.
  881. raise OSError(err, 'Connect call failed %s' % (address,))
  882. except (BlockingIOError, InterruptedError):
  883. # socket is still registered, the callback will be retried later
  884. pass
  885. except (KeyboardInterrupt, SystemExit):
  886. raise
  887. except BaseException as exc:
  888. fut.set_exception(exc)
  889. self._remove_writer(sock)
  890. else:
  891. fut.set_result(None)
  892. self._remove_writer(sock)
  893. cdef _sock_set_reuseport(self, int fd):
  894. cdef:
  895. int err = 0
  896. int reuseport_flag = 1
  897. err = system.setsockopt(
  898. fd,
  899. uv.SOL_SOCKET,
  900. SO_REUSEPORT,
  901. <char*>&reuseport_flag,
  902. sizeof(reuseport_flag))
  903. if err < 0:
  904. raise convert_error(-errno.errno)
  905. cdef _set_coroutine_debug(self, bint enabled):
  906. enabled = bool(enabled)
  907. if self._coroutine_debug_set == enabled:
  908. return
  909. if enabled:
  910. self._coroutine_origin_tracking_saved_depth = (
  911. sys.get_coroutine_origin_tracking_depth())
  912. sys.set_coroutine_origin_tracking_depth(
  913. DEBUG_STACK_DEPTH)
  914. else:
  915. sys.set_coroutine_origin_tracking_depth(
  916. self._coroutine_origin_tracking_saved_depth)
  917. self._coroutine_debug_set = enabled
  918. def _get_backend_id(self):
  919. """This method is used by uvloop tests and is not part of the API."""
  920. return uv.uv_backend_fd(self.uvloop)
  921. cdef _print_debug_info(self):
  922. cdef:
  923. int err
  924. uv.uv_rusage_t rusage
  925. err = uv.uv_getrusage(&rusage)
  926. if err < 0:
  927. raise convert_error(err)
  928. # OS
  929. print('---- Process info: -----')
  930. print('Process memory: {}'.format(rusage.ru_maxrss))
  931. print('Number of signals: {}'.format(rusage.ru_nsignals))
  932. print('')
  933. # Loop
  934. print('--- Loop debug info: ---')
  935. print('Loop time: {}'.format(self.time()))
  936. print('Errors logged: {}'.format(
  937. self._debug_exception_handler_cnt))
  938. print()
  939. print('Callback handles: {: <8} | {}'.format(
  940. self._debug_cb_handles_count,
  941. self._debug_cb_handles_total))
  942. print('Timer handles: {: <8} | {}'.format(
  943. self._debug_cb_timer_handles_count,
  944. self._debug_cb_timer_handles_total))
  945. print()
  946. print(' alive | closed |')
  947. print('UVHandles python | libuv | total')
  948. print(' objs | handles |')
  949. print('-------------------------------+---------+---------')
  950. for name in sorted(self._debug_handles_total):
  951. print(' {: <18} {: >7} | {: >7} | {: >7}'.format(
  952. name,
  953. self._debug_handles_current[name],
  954. self._debug_handles_closed[name],
  955. self._debug_handles_total[name]))
  956. print()
  957. print('uv_handle_t (current: {}; freed: {}; total: {})'.format(
  958. self._debug_uv_handles_total - self._debug_uv_handles_freed,
  959. self._debug_uv_handles_freed,
  960. self._debug_uv_handles_total))
  961. print()
  962. print('--- Streams debug info: ---')
  963. print('Write errors: {}'.format(
  964. self._debug_stream_write_errors_total))
  965. print('Write without poll: {}'.format(
  966. self._debug_stream_write_tries))
  967. print('Write contexts: {: <8} | {}'.format(
  968. self._debug_stream_write_ctx_cnt,
  969. self._debug_stream_write_ctx_total))
  970. print('Write failed callbacks: {}'.format(
  971. self._debug_stream_write_cb_errors_total))
  972. print()
  973. print('Read errors: {}'.format(
  974. self._debug_stream_read_errors_total))
  975. print('Read callbacks: {}'.format(
  976. self._debug_stream_read_cb_total))
  977. print('Read failed callbacks: {}'.format(
  978. self._debug_stream_read_cb_errors_total))
  979. print('Read EOFs: {}'.format(
  980. self._debug_stream_read_eof_total))
  981. print('Read EOF failed callbacks: {}'.format(
  982. self._debug_stream_read_eof_cb_errors_total))
  983. print()
  984. print('Listen errors: {}'.format(
  985. self._debug_stream_listen_errors_total))
  986. print('Shutdown errors {}'.format(
  987. self._debug_stream_shutdown_errors_total))
  988. print()
  989. print('--- Polls debug info: ---')
  990. print('Read events: {}'.format(
  991. self._poll_read_events_total))
  992. print('Read callbacks failed: {}'.format(
  993. self._poll_read_cb_errors_total))
  994. print('Write events: {}'.format(
  995. self._poll_write_events_total))
  996. print('Write callbacks failed: {}'.format(
  997. self._poll_write_cb_errors_total))
  998. print()
  999. print('--- Sock ops successful on 1st try: ---')
  1000. print('Socket try-writes: {}'.format(
  1001. self._sock_try_write_total))
  1002. print(flush=True)
  1003. property print_debug_info:
  1004. def __get__(self):
  1005. if UVLOOP_DEBUG:
  1006. return lambda: self._print_debug_info()
  1007. else:
  1008. raise AttributeError('print_debug_info')
  1009. # Public API
  1010. def __repr__(self):
  1011. return '<{}.{} running={} closed={} debug={}>'.format(
  1012. self.__class__.__module__,
  1013. self.__class__.__name__,
  1014. self.is_running(),
  1015. self.is_closed(),
  1016. self.get_debug()
  1017. )
  1018. def call_soon(self, callback, *args, context=None):
  1019. """Arrange for a callback to be called as soon as possible.
  1020. This operates as a FIFO queue: callbacks are called in the
  1021. order in which they are registered. Each callback will be
  1022. called exactly once.
  1023. Any positional arguments after the callback will be passed to
  1024. the callback when it is called.
  1025. """
  1026. if self._debug == 1:
  1027. self._check_thread()
  1028. if args:
  1029. return self._call_soon(callback, args, context)
  1030. else:
  1031. return self._call_soon(callback, None, context)
  1032. def call_soon_threadsafe(self, callback, *args, context=None):
  1033. """Like call_soon(), but thread-safe."""
  1034. if not args:
  1035. args = None
  1036. cdef Handle handle = new_Handle(self, callback, args, context)
  1037. self._append_ready_handle(handle) # deque append is atomic
  1038. # libuv async handler is thread-safe while the idle handler is not -
  1039. # we only set the async handler here, which will start the idle handler
  1040. # in _on_wake() from the loop and eventually call the callback.
  1041. self.handler_async.send()
  1042. return handle
  1043. def call_later(self, delay, callback, *args, context=None):
  1044. """Arrange for a callback to be called at a given time.
  1045. Return a Handle: an opaque object with a cancel() method that
  1046. can be used to cancel the call.
  1047. The delay can be an int or float, expressed in seconds. It is
  1048. always relative to the current time.
  1049. Each callback will be called exactly once. If two callbacks
  1050. are scheduled for exactly the same time, it undefined which
  1051. will be called first.
  1052. Any positional arguments after the callback will be passed to
  1053. the callback when it is called.
  1054. """
  1055. cdef uint64_t when
  1056. self._check_closed()
  1057. if self._debug == 1:
  1058. self._check_thread()
  1059. if delay < 0:
  1060. delay = 0
  1061. elif delay == py_inf or delay > MAX_SLEEP:
  1062. # ~100 years sounds like a good approximation of
  1063. # infinity for a Python application.
  1064. delay = MAX_SLEEP
  1065. when = <uint64_t>round(delay * 1000)
  1066. if not args:
  1067. args = None
  1068. if when == 0:
  1069. return self._call_soon(callback, args, context)
  1070. else:
  1071. return self._call_later(when, callback, args, context)
  1072. def call_at(self, when, callback, *args, context=None):
  1073. """Like call_later(), but uses an absolute time.
  1074. Absolute time corresponds to the event loop's time() method.
  1075. """
  1076. return self.call_later(
  1077. when - self.time(), callback, *args, context=context)
  1078. def time(self):
  1079. """Return the time according to the event loop's clock.
  1080. This is a float expressed in seconds since an epoch, but the
  1081. epoch, precision, accuracy and drift are unspecified and may
  1082. differ per event loop.
  1083. """
  1084. return self._time() / 1000
  1085. def stop(self):
  1086. """Stop running the event loop.
  1087. Every callback already scheduled will still run. This simply informs
  1088. run_forever to stop looping after a complete iteration.
  1089. """
  1090. self._call_soon_handle(
  1091. new_MethodHandle1(
  1092. self,
  1093. "Loop._stop",
  1094. <method1_t>self._stop,
  1095. None,
  1096. self,
  1097. None))
  1098. def run_forever(self):
  1099. """Run the event loop until stop() is called."""
  1100. self._check_closed()
  1101. mode = uv.UV_RUN_DEFAULT
  1102. if self._stopping:
  1103. # loop.stop() was called right before loop.run_forever().
  1104. # This is how asyncio loop behaves.
  1105. mode = uv.UV_RUN_NOWAIT
  1106. self._set_coroutine_debug(self._debug)
  1107. old_agen_hooks = sys.get_asyncgen_hooks()
  1108. sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
  1109. finalizer=self._asyncgen_finalizer_hook)
  1110. try:
  1111. self._run(mode)
  1112. finally:
  1113. self._set_coroutine_debug(False)
  1114. sys.set_asyncgen_hooks(*old_agen_hooks)
  1115. def close(self):
  1116. """Close the event loop.
  1117. The event loop must not be running.
  1118. This is idempotent and irreversible.
  1119. No other methods should be called after this one.
  1120. """
  1121. self._close()
  1122. def get_debug(self):
  1123. return bool(self._debug)
  1124. def set_debug(self, enabled):
  1125. self._debug = bool(enabled)
  1126. if self.is_running():
  1127. self.call_soon_threadsafe(self._set_coroutine_debug, self._debug)
  1128. def is_running(self):
  1129. """Return whether the event loop is currently running."""
  1130. return bool(self._running)
  1131. def is_closed(self):
  1132. """Returns True if the event loop was closed."""
  1133. return bool(self._closed)
  1134. def create_future(self):
  1135. """Create a Future object attached to the loop."""
  1136. return self._new_future()
  1137. def create_task(self, coro, *, name=None, context=None):
  1138. """Schedule a coroutine object.
  1139. Return a task object.
  1140. If name is not None, task.set_name(name) will be called if the task
  1141. object has the set_name attribute, true for default Task in CPython.
  1142. An optional keyword-only context argument allows specifying a custom
  1143. contextvars.Context for the coro to run in. The current context copy is
  1144. created when no context is provided.
  1145. """
  1146. self._check_closed()
  1147. if PY311:
  1148. if self._task_factory is None:
  1149. task = aio_Task(coro, loop=self, context=context)
  1150. else:
  1151. task = self._task_factory(self, coro, context=context)
  1152. else:
  1153. if context is None:
  1154. if self._task_factory is None:
  1155. task = aio_Task(coro, loop=self)
  1156. else:
  1157. task = self._task_factory(self, coro)
  1158. else:
  1159. if self._task_factory is None:
  1160. task = context.run(aio_Task, coro, self)
  1161. else:
  1162. task = context.run(self._task_factory, self, coro)
  1163. # copied from asyncio.tasks._set_task_name (bpo-34270)
  1164. if name is not None:
  1165. try:
  1166. set_name = task.set_name
  1167. except AttributeError:
  1168. pass
  1169. else:
  1170. set_name(name)
  1171. return task
  1172. def set_task_factory(self, factory):
  1173. """Set a task factory that will be used by loop.create_task().
  1174. If factory is None the default task factory will be set.
  1175. If factory is a callable, it should have a signature matching
  1176. '(loop, coro)', where 'loop' will be a reference to the active
  1177. event loop, 'coro' will be a coroutine object. The callable
  1178. must return a Future.
  1179. """
  1180. if factory is not None and not callable(factory):
  1181. raise TypeError('task factory must be a callable or None')
  1182. self._task_factory = factory
  1183. def get_task_factory(self):
  1184. """Return a task factory, or None if the default one is in use."""
  1185. return self._task_factory
  1186. def run_until_complete(self, future):
  1187. """Run until the Future is done.
  1188. If the argument is a coroutine, it is wrapped in a Task.
  1189. WARNING: It would be disastrous to call run_until_complete()
  1190. with the same coroutine twice -- it would wrap it in two
  1191. different Tasks and that can't be good.
  1192. Return the Future's result, or raise its exception.
  1193. """
  1194. self._check_closed()
  1195. new_task = not isfuture(future)
  1196. future = aio_ensure_future(future, loop=self)
  1197. if new_task:
  1198. # An exception is raised if the future didn't complete, so there
  1199. # is no need to log the "destroy pending task" message
  1200. future._log_destroy_pending = False
  1201. def done_cb(fut):
  1202. if not fut.cancelled():
  1203. exc = fut.exception()
  1204. if isinstance(exc, (SystemExit, KeyboardInterrupt)):
  1205. # Issue #336: run_forever() already finished,
  1206. # no need to stop it.
  1207. return
  1208. self.stop()
  1209. future.add_done_callback(done_cb)
  1210. try:
  1211. self.run_forever()
  1212. except BaseException:
  1213. if new_task and future.done() and not future.cancelled():
  1214. # The coroutine raised a BaseException. Consume the exception
  1215. # to not log a warning, the caller doesn't have access to the
  1216. # local task.
  1217. future.exception()
  1218. raise
  1219. finally:
  1220. future.remove_done_callback(done_cb)
  1221. if not future.done():
  1222. raise RuntimeError('Event loop stopped before Future completed.')
  1223. return future.result()
  1224. @cython.iterable_coroutine
  1225. async def getaddrinfo(self, object host, object port, *,
  1226. int family=0, int type=0, int proto=0, int flags=0):
  1227. addr = __static_getaddrinfo_pyaddr(host, port, family,
  1228. type, proto, flags)
  1229. if addr is not None:
  1230. return [addr]
  1231. return await self._getaddrinfo(
  1232. host, port, family, type, proto, flags, 1)
  1233. @cython.iterable_coroutine
  1234. async def getnameinfo(self, sockaddr, int flags=0):
  1235. cdef:
  1236. AddrInfo ai_cnt
  1237. system.addrinfo *ai
  1238. system.sockaddr_in6 *sin6
  1239. if not isinstance(sockaddr, tuple):
  1240. raise TypeError('getnameinfo() argument 1 must be a tuple')
  1241. sl = len(sockaddr)
  1242. if sl < 2 or sl > 4:
  1243. raise ValueError('sockaddr must be a tuple of 2, 3 or 4 values')
  1244. if sl > 2:
  1245. flowinfo = sockaddr[2]
  1246. if flowinfo < 0 or flowinfo > 0xfffff:
  1247. raise OverflowError(
  1248. 'getnameinfo(): flowinfo must be 0-1048575.')
  1249. else:
  1250. flowinfo = 0
  1251. if sl > 3:
  1252. scope_id = sockaddr[3]
  1253. if scope_id < 0 or scope_id > 2 ** 32:
  1254. raise OverflowError(
  1255. 'getsockaddrarg: scope_id must be unsigned 32 bit integer')
  1256. else:
  1257. scope_id = 0
  1258. ai_cnt = await self._getaddrinfo(
  1259. sockaddr[0], sockaddr[1],
  1260. uv.AF_UNSPEC, # family
  1261. uv.SOCK_DGRAM, # type
  1262. 0, # proto
  1263. uv.AI_NUMERICHOST, # flags
  1264. 0) # unpack
  1265. ai = ai_cnt.data
  1266. if ai.ai_next:
  1267. raise OSError("sockaddr resolved to multiple addresses")
  1268. if ai.ai_family == uv.AF_INET:
  1269. if sl > 2:
  1270. raise OSError("IPv4 sockaddr must be 2 tuple")
  1271. elif ai.ai_family == uv.AF_INET6:
  1272. # Modify some fields in `ai`
  1273. sin6 = <system.sockaddr_in6*> ai.ai_addr
  1274. sin6.sin6_flowinfo = system.htonl(flowinfo)
  1275. sin6.sin6_scope_id = scope_id
  1276. return await self._getnameinfo(ai.ai_addr, flags)
  1277. @cython.iterable_coroutine
  1278. async def start_tls(self, transport, protocol, sslcontext, *,
  1279. server_side=False,
  1280. server_hostname=None,
  1281. ssl_handshake_timeout=None,
  1282. ssl_shutdown_timeout=None):
  1283. """Upgrade transport to TLS.
  1284. Return a new transport that *protocol* should start using
  1285. immediately.
  1286. """
  1287. if not isinstance(sslcontext, ssl_SSLContext):
  1288. raise TypeError(
  1289. f'sslcontext is expected to be an instance of ssl.SSLContext, '
  1290. f'got {sslcontext!r}')
  1291. if isinstance(transport, (TCPTransport, UnixTransport)):
  1292. context = (<UVStream>transport).context
  1293. elif isinstance(transport, _SSLProtocolTransport):
  1294. context = (<_SSLProtocolTransport>transport).context
  1295. else:
  1296. raise TypeError(
  1297. f'transport {transport!r} is not supported by start_tls()')
  1298. waiter = self._new_future()
  1299. ssl_protocol = SSLProtocol(
  1300. self, protocol, sslcontext, waiter,
  1301. server_side, server_hostname,
  1302. ssl_handshake_timeout=ssl_handshake_timeout,
  1303. ssl_shutdown_timeout=ssl_shutdown_timeout,
  1304. call_connection_made=False)
  1305. # Pause early so that "ssl_protocol.data_received()" doesn't
  1306. # have a chance to get called before "ssl_protocol.connection_made()".
  1307. transport.pause_reading()
  1308. transport.set_protocol(ssl_protocol)
  1309. conmade_cb = self.call_soon(ssl_protocol.connection_made, transport,
  1310. context=context)
  1311. # transport.resume_reading() will use the right context
  1312. # (transport.context) to call e.g. data_received()
  1313. resume_cb = self.call_soon(transport.resume_reading)
  1314. app_transport = ssl_protocol._get_app_transport(context)
  1315. try:
  1316. await waiter
  1317. except (KeyboardInterrupt, SystemExit):
  1318. raise
  1319. except BaseException:
  1320. app_transport.close()
  1321. conmade_cb.cancel()
  1322. resume_cb.cancel()
  1323. raise
  1324. return app_transport
  1325. @cython.iterable_coroutine
  1326. async def create_server(self, protocol_factory, host=None, port=None,
  1327. *,
  1328. int family=uv.AF_UNSPEC,
  1329. int flags=uv.AI_PASSIVE,
  1330. sock=None,
  1331. backlog=100,
  1332. ssl=None,
  1333. reuse_address=None,
  1334. reuse_port=None,
  1335. ssl_handshake_timeout=None,
  1336. ssl_shutdown_timeout=None,
  1337. start_serving=True):
  1338. """A coroutine which creates a TCP server bound to host and port.
  1339. The return value is a Server object which can be used to stop
  1340. the service.
  1341. If host is an empty string or None all interfaces are assumed
  1342. and a list of multiple sockets will be returned (most likely
  1343. one for IPv4 and another one for IPv6). The host parameter can also be
  1344. a sequence (e.g. list) of hosts to bind to.
  1345. family can be set to either AF_INET or AF_INET6 to force the
  1346. socket to use IPv4 or IPv6. If not set it will be determined
  1347. from host (defaults to AF_UNSPEC).
  1348. flags is a bitmask for getaddrinfo().
  1349. sock can optionally be specified in order to use a preexisting
  1350. socket object.
  1351. backlog is the maximum number of queued connections passed to
  1352. listen() (defaults to 100).
  1353. ssl can be set to an SSLContext to enable SSL over the
  1354. accepted connections.
  1355. reuse_address tells the kernel to reuse a local socket in
  1356. TIME_WAIT state, without waiting for its natural timeout to
  1357. expire. If not specified will automatically be set to True on
  1358. UNIX.
  1359. reuse_port tells the kernel to allow this endpoint to be bound to
  1360. the same port as other existing endpoints are bound to, so long as
  1361. they all set this flag when being created. This option is not
  1362. supported on Windows.
  1363. ssl_handshake_timeout is the time in seconds that an SSL server
  1364. will wait for completion of the SSL handshake before aborting the
  1365. connection. Default is 60s.
  1366. ssl_shutdown_timeout is the time in seconds that an SSL server
  1367. will wait for completion of the SSL shutdown before aborting the
  1368. connection. Default is 30s.
  1369. """
  1370. cdef:
  1371. TCPServer tcp
  1372. system.addrinfo *addrinfo
  1373. Server server
  1374. if sock is not None and sock.family == uv.AF_UNIX:
  1375. if host is not None or port is not None:
  1376. raise ValueError(
  1377. 'host/port and sock can not be specified at the same time')
  1378. return await self.create_unix_server(
  1379. protocol_factory, sock=sock, backlog=backlog, ssl=ssl,
  1380. start_serving=start_serving,
  1381. # asyncio won't clean up socket file using create_server() API
  1382. cleanup_socket=False,
  1383. )
  1384. server = Server(self)
  1385. if ssl is not None:
  1386. if not isinstance(ssl, ssl_SSLContext):
  1387. raise TypeError('ssl argument must be an SSLContext or None')
  1388. else:
  1389. if ssl_handshake_timeout is not None:
  1390. raise ValueError(
  1391. 'ssl_handshake_timeout is only meaningful with ssl')
  1392. if ssl_shutdown_timeout is not None:
  1393. raise ValueError(
  1394. 'ssl_shutdown_timeout is only meaningful with ssl')
  1395. if host is not None or port is not None:
  1396. if sock is not None:
  1397. raise ValueError(
  1398. 'host/port and sock can not be specified at the same time')
  1399. if reuse_address is None:
  1400. reuse_address = os_name == 'posix' and sys_platform != 'cygwin'
  1401. reuse_port = bool(reuse_port)
  1402. if reuse_port and not has_SO_REUSEPORT:
  1403. raise ValueError(
  1404. 'reuse_port not supported by socket module')
  1405. if host == '':
  1406. hosts = [None]
  1407. elif (isinstance(host, str) or not isinstance(host, col_Iterable)):
  1408. hosts = [host]
  1409. else:
  1410. hosts = host
  1411. fs = [self._getaddrinfo(host, port, family,
  1412. uv.SOCK_STREAM, 0, flags,
  1413. 0) for host in hosts]
  1414. infos = await aio_gather(*fs)
  1415. completed = False
  1416. sock = None
  1417. try:
  1418. for info in infos:
  1419. addrinfo = (<AddrInfo>info).data
  1420. while addrinfo != NULL:
  1421. if addrinfo.ai_family == uv.AF_UNSPEC:
  1422. raise RuntimeError('AF_UNSPEC in DNS results')
  1423. try:
  1424. sock = socket_socket(addrinfo.ai_family,
  1425. addrinfo.ai_socktype,
  1426. addrinfo.ai_protocol)
  1427. except socket_error:
  1428. # Assume it's a bad family/type/protocol
  1429. # combination.
  1430. if self._debug:
  1431. aio_logger.warning(
  1432. 'create_server() failed to create '
  1433. 'socket.socket(%r, %r, %r)',
  1434. addrinfo.ai_family,
  1435. addrinfo.ai_socktype,
  1436. addrinfo.ai_protocol, exc_info=True)
  1437. addrinfo = addrinfo.ai_next
  1438. continue
  1439. if reuse_address:
  1440. sock.setsockopt(uv.SOL_SOCKET, uv.SO_REUSEADDR, 1)
  1441. if reuse_port:
  1442. sock.setsockopt(uv.SOL_SOCKET, SO_REUSEPORT, 1)
  1443. # Disable IPv4/IPv6 dual stack support (enabled by
  1444. # default on Linux) which makes a single socket
  1445. # listen on both address families.
  1446. if (addrinfo.ai_family == uv.AF_INET6 and
  1447. has_IPV6_V6ONLY):
  1448. sock.setsockopt(uv.IPPROTO_IPV6, IPV6_V6ONLY, 1)
  1449. pyaddr = __convert_sockaddr_to_pyaddr(addrinfo.ai_addr)
  1450. try:
  1451. sock.bind(pyaddr)
  1452. except OSError as err:
  1453. raise OSError(
  1454. err.errno, 'error while attempting '
  1455. 'to bind on address %r: %s'
  1456. % (pyaddr, err.strerror.lower())) from None
  1457. tcp = TCPServer.new(self, protocol_factory, server,
  1458. uv.AF_UNSPEC, backlog,
  1459. ssl, ssl_handshake_timeout,
  1460. ssl_shutdown_timeout)
  1461. try:
  1462. tcp._open(sock.fileno())
  1463. except (KeyboardInterrupt, SystemExit):
  1464. raise
  1465. except BaseException:
  1466. tcp._close()
  1467. raise
  1468. server._add_server(tcp)
  1469. sock.detach()
  1470. sock = None
  1471. addrinfo = addrinfo.ai_next
  1472. completed = True
  1473. finally:
  1474. if not completed:
  1475. if sock is not None:
  1476. sock.close()
  1477. server.close()
  1478. else:
  1479. if sock is None:
  1480. raise ValueError('Neither host/port nor sock were specified')
  1481. if not _is_sock_stream(sock.type):
  1482. raise ValueError(
  1483. 'A Stream Socket was expected, got {!r}'.format(sock))
  1484. # libuv will set the socket to non-blocking mode, but
  1485. # we want Python socket object to notice that.
  1486. sock.setblocking(False)
  1487. tcp = TCPServer.new(self, protocol_factory, server,
  1488. uv.AF_UNSPEC, backlog,
  1489. ssl, ssl_handshake_timeout,
  1490. ssl_shutdown_timeout)
  1491. try:
  1492. tcp._open(sock.fileno())
  1493. except (KeyboardInterrupt, SystemExit):
  1494. raise
  1495. except BaseException:
  1496. tcp._close()
  1497. raise
  1498. tcp._attach_fileobj(sock)
  1499. server._add_server(tcp)
  1500. if start_serving:
  1501. server._start_serving()
  1502. server._ref()
  1503. return server
  1504. @cython.iterable_coroutine
  1505. async def create_connection(self, protocol_factory, host=None, port=None,
  1506. *,
  1507. ssl=None,
  1508. family=0, proto=0, flags=0, sock=None,
  1509. local_addr=None, server_hostname=None,
  1510. ssl_handshake_timeout=None,
  1511. ssl_shutdown_timeout=None):
  1512. """Connect to a TCP server.
  1513. Create a streaming transport connection to a given Internet host and
  1514. port: socket family AF_INET or socket.AF_INET6 depending on host (or
  1515. family if specified), socket type SOCK_STREAM. protocol_factory must be
  1516. a callable returning a protocol instance.
  1517. This method is a coroutine which will try to establish the connection
  1518. in the background. When successful, the coroutine returns a
  1519. (transport, protocol) pair.
  1520. """
  1521. cdef:
  1522. AddrInfo ai_local = None
  1523. AddrInfo ai_remote
  1524. TCPTransport tr
  1525. system.addrinfo *rai = NULL
  1526. system.addrinfo *lai = NULL
  1527. system.addrinfo *rai_iter = NULL
  1528. system.addrinfo *lai_iter = NULL
  1529. system.addrinfo rai_static
  1530. system.sockaddr_storage rai_addr_static
  1531. system.addrinfo lai_static
  1532. system.sockaddr_storage lai_addr_static
  1533. object app_protocol
  1534. object app_transport
  1535. object protocol
  1536. object ssl_waiter
  1537. if sock is not None and sock.family == uv.AF_UNIX:
  1538. if host is not None or port is not None:
  1539. raise ValueError(
  1540. 'host/port and sock can not be specified at the same time')
  1541. return await self.create_unix_connection(
  1542. protocol_factory, None,
  1543. sock=sock, ssl=ssl, server_hostname=server_hostname)
  1544. app_protocol = protocol = protocol_factory()
  1545. ssl_waiter = None
  1546. context = Context_CopyCurrent()
  1547. if ssl:
  1548. if server_hostname is None:
  1549. if not host:
  1550. raise ValueError('You must set server_hostname '
  1551. 'when using ssl without a host')
  1552. server_hostname = host
  1553. ssl_waiter = self._new_future()
  1554. sslcontext = None if isinstance(ssl, bool) else ssl
  1555. protocol = SSLProtocol(
  1556. self, app_protocol, sslcontext, ssl_waiter,
  1557. False, server_hostname,
  1558. ssl_handshake_timeout=ssl_handshake_timeout,
  1559. ssl_shutdown_timeout=ssl_shutdown_timeout)
  1560. else:
  1561. if server_hostname is not None:
  1562. raise ValueError('server_hostname is only meaningful with ssl')
  1563. if ssl_handshake_timeout is not None:
  1564. raise ValueError(
  1565. 'ssl_handshake_timeout is only meaningful with ssl')
  1566. if ssl_shutdown_timeout is not None:
  1567. raise ValueError(
  1568. 'ssl_shutdown_timeout is only meaningful with ssl')
  1569. if host is not None or port is not None:
  1570. if sock is not None:
  1571. raise ValueError(
  1572. 'host/port and sock can not be specified at the same time')
  1573. fs = []
  1574. f1 = f2 = None
  1575. addr = __static_getaddrinfo(
  1576. host, port, family, uv.SOCK_STREAM,
  1577. proto, <system.sockaddr*>&rai_addr_static)
  1578. if addr is None:
  1579. f1 = self._getaddrinfo(
  1580. host, port, family,
  1581. uv.SOCK_STREAM, proto, flags,
  1582. 0) # 0 == don't unpack
  1583. fs.append(f1)
  1584. else:
  1585. rai_static.ai_addr = <system.sockaddr*>&rai_addr_static
  1586. rai_static.ai_next = NULL
  1587. rai = &rai_static
  1588. if local_addr is not None:
  1589. if not isinstance(local_addr, (tuple, list)) or \
  1590. len(local_addr) != 2:
  1591. raise ValueError(
  1592. 'local_addr must be a tuple of host and port')
  1593. addr = __static_getaddrinfo(
  1594. local_addr[0], local_addr[1],
  1595. family, uv.SOCK_STREAM,
  1596. proto, <system.sockaddr*>&lai_addr_static)
  1597. if addr is None:
  1598. f2 = self._getaddrinfo(
  1599. local_addr[0], local_addr[1], family,
  1600. uv.SOCK_STREAM, proto, flags,
  1601. 0) # 0 == don't unpack
  1602. fs.append(f2)
  1603. else:
  1604. lai_static.ai_addr = <system.sockaddr*>&lai_addr_static
  1605. lai_static.ai_next = NULL
  1606. lai = &lai_static
  1607. if len(fs):
  1608. await aio_wait(fs)
  1609. if rai is NULL:
  1610. ai_remote = f1.result()
  1611. if ai_remote.data is NULL:
  1612. raise OSError('getaddrinfo() returned empty list')
  1613. rai = ai_remote.data
  1614. if lai is NULL and f2 is not None:
  1615. ai_local = f2.result()
  1616. if ai_local.data is NULL:
  1617. raise OSError(
  1618. 'getaddrinfo() returned empty list for local_addr')
  1619. lai = ai_local.data
  1620. exceptions = []
  1621. rai_iter = rai
  1622. while rai_iter is not NULL:
  1623. tr = None
  1624. try:
  1625. waiter = self._new_future()
  1626. tr = TCPTransport.new(self, protocol, None, waiter,
  1627. context)
  1628. if lai is not NULL:
  1629. lai_iter = lai
  1630. while lai_iter is not NULL:
  1631. try:
  1632. tr.bind(lai_iter.ai_addr)
  1633. break
  1634. except OSError as exc:
  1635. exceptions.append(exc)
  1636. lai_iter = lai_iter.ai_next
  1637. else:
  1638. tr._close()
  1639. tr = None
  1640. rai_iter = rai_iter.ai_next
  1641. continue
  1642. tr.connect(rai_iter.ai_addr)
  1643. await waiter
  1644. except OSError as exc:
  1645. if tr is not None:
  1646. tr._close()
  1647. tr = None
  1648. exceptions.append(exc)
  1649. except (KeyboardInterrupt, SystemExit):
  1650. raise
  1651. except BaseException:
  1652. if tr is not None:
  1653. tr._close()
  1654. tr = None
  1655. raise
  1656. else:
  1657. break
  1658. rai_iter = rai_iter.ai_next
  1659. else:
  1660. # If they all have the same str(), raise one.
  1661. model = str(exceptions[0])
  1662. if all(str(exc) == model for exc in exceptions):
  1663. raise exceptions[0]
  1664. # Raise a combined exception so the user can see all
  1665. # the various error messages.
  1666. raise OSError('Multiple exceptions: {}'.format(
  1667. ', '.join(str(exc) for exc in exceptions)))
  1668. else:
  1669. if sock is None:
  1670. raise ValueError(
  1671. 'host and port was not specified and no sock specified')
  1672. if not _is_sock_stream(sock.type):
  1673. raise ValueError(
  1674. 'A Stream Socket was expected, got {!r}'.format(sock))
  1675. # libuv will set the socket to non-blocking mode, but
  1676. # we want Python socket object to notice that.
  1677. sock.setblocking(False)
  1678. waiter = self._new_future()
  1679. tr = TCPTransport.new(self, protocol, None, waiter, context)
  1680. try:
  1681. # libuv will make socket non-blocking
  1682. tr._open(sock.fileno())
  1683. tr._init_protocol()
  1684. await waiter
  1685. except (KeyboardInterrupt, SystemExit):
  1686. raise
  1687. except BaseException:
  1688. # It's OK to call `_close()` here, as opposed to
  1689. # `_force_close()` or `close()` as we want to terminate the
  1690. # transport immediately. The `waiter` can only be waken
  1691. # up in `Transport._call_connection_made()`, and calling
  1692. # `_close()` before it is fine.
  1693. tr._close()
  1694. raise
  1695. tr._attach_fileobj(sock)
  1696. if ssl:
  1697. app_transport = protocol._get_app_transport(context)
  1698. try:
  1699. await ssl_waiter
  1700. except (KeyboardInterrupt, SystemExit):
  1701. raise
  1702. except BaseException:
  1703. app_transport.close()
  1704. raise
  1705. return app_transport, app_protocol
  1706. else:
  1707. return tr, protocol
  1708. @cython.iterable_coroutine
  1709. async def create_unix_server(self, protocol_factory, path=None,
  1710. *, backlog=100, sock=None, ssl=None,
  1711. ssl_handshake_timeout=None,
  1712. ssl_shutdown_timeout=None,
  1713. start_serving=True, cleanup_socket=PY313):
  1714. """A coroutine which creates a UNIX Domain Socket server.
  1715. The return value is a Server object, which can be used to stop
  1716. the service.
  1717. path is a str, representing a file systsem path to bind the
  1718. server socket to.
  1719. sock can optionally be specified in order to use a preexisting
  1720. socket object.
  1721. backlog is the maximum number of queued connections passed to
  1722. listen() (defaults to 100).
  1723. ssl can be set to an SSLContext to enable SSL over the
  1724. accepted connections.
  1725. ssl_handshake_timeout is the time in seconds that an SSL server
  1726. will wait for completion of the SSL handshake before aborting the
  1727. connection. Default is 60s.
  1728. ssl_shutdown_timeout is the time in seconds that an SSL server
  1729. will wait for completion of the SSL shutdown before aborting the
  1730. connection. Default is 30s.
  1731. If *cleanup_socket* is true then the Unix socket will automatically
  1732. be removed from the filesystem when the server is closed, unless the
  1733. socket has been replaced after the server has been created.
  1734. This defaults to True on Python 3.13 and above, or False otherwise.
  1735. """
  1736. cdef:
  1737. UnixServer pipe
  1738. Server server = Server(self)
  1739. if ssl is not None:
  1740. if not isinstance(ssl, ssl_SSLContext):
  1741. raise TypeError('ssl argument must be an SSLContext or None')
  1742. else:
  1743. if ssl_handshake_timeout is not None:
  1744. raise ValueError(
  1745. 'ssl_handshake_timeout is only meaningful with ssl')
  1746. if ssl_shutdown_timeout is not None:
  1747. raise ValueError(
  1748. 'ssl_shutdown_timeout is only meaningful with ssl')
  1749. if path is not None:
  1750. if sock is not None:
  1751. raise ValueError(
  1752. 'path and sock can not be specified at the same time')
  1753. orig_path = path
  1754. path = os_fspath(path)
  1755. if isinstance(path, str):
  1756. path = PyUnicode_EncodeFSDefault(path)
  1757. # Check for abstract socket.
  1758. if path[0] != 0:
  1759. try:
  1760. if stat_S_ISSOCK(os_stat(path).st_mode):
  1761. os_remove(path)
  1762. except FileNotFoundError:
  1763. pass
  1764. except OSError as err:
  1765. # Directory may have permissions only to create socket.
  1766. aio_logger.error(
  1767. 'Unable to check or remove stale UNIX socket %r: %r',
  1768. orig_path, err)
  1769. # We use Python sockets to create a UNIX server socket because
  1770. # when UNIX sockets are created by libuv, libuv removes the path
  1771. # they were bound to. This is different from asyncio, which
  1772. # doesn't cleanup the socket path.
  1773. sock = socket_socket(uv.AF_UNIX)
  1774. try:
  1775. sock.bind(path)
  1776. except OSError as exc:
  1777. sock.close()
  1778. if exc.errno == errno.EADDRINUSE:
  1779. # Let's improve the error message by adding
  1780. # with what exact address it occurs.
  1781. msg = 'Address {!r} is already in use'.format(orig_path)
  1782. raise OSError(errno.EADDRINUSE, msg) from None
  1783. else:
  1784. raise
  1785. except (KeyboardInterrupt, SystemExit):
  1786. raise
  1787. except BaseException:
  1788. sock.close()
  1789. raise
  1790. else:
  1791. if sock is None:
  1792. raise ValueError(
  1793. 'path was not specified, and no sock specified')
  1794. if sock.family != uv.AF_UNIX or not _is_sock_stream(sock.type):
  1795. raise ValueError(
  1796. 'A UNIX Domain Stream Socket was expected, got {!r}'
  1797. .format(sock))
  1798. # libuv will set the socket to non-blocking mode, but
  1799. # we want Python socket object to notice that.
  1800. sock.setblocking(False)
  1801. if cleanup_socket:
  1802. path = sock.getsockname()
  1803. # Check for abstract socket. `str` and `bytes` paths are supported.
  1804. if path[0] not in (0, '\x00'):
  1805. try:
  1806. self._unix_server_sockets[sock] = os_stat(path).st_ino
  1807. except FileNotFoundError:
  1808. pass
  1809. pipe = UnixServer.new(
  1810. self, protocol_factory, server, backlog,
  1811. ssl, ssl_handshake_timeout, ssl_shutdown_timeout)
  1812. try:
  1813. pipe._open(sock.fileno())
  1814. except (KeyboardInterrupt, SystemExit):
  1815. raise
  1816. except BaseException:
  1817. pipe._close()
  1818. sock.close()
  1819. raise
  1820. pipe._attach_fileobj(sock)
  1821. server._add_server(pipe)
  1822. if start_serving:
  1823. server._start_serving()
  1824. return server
  1825. @cython.iterable_coroutine
  1826. async def create_unix_connection(self, protocol_factory, path=None, *,
  1827. ssl=None, sock=None,
  1828. server_hostname=None,
  1829. ssl_handshake_timeout=None,
  1830. ssl_shutdown_timeout=None):
  1831. cdef:
  1832. UnixTransport tr
  1833. object app_protocol
  1834. object app_transport
  1835. object protocol
  1836. object ssl_waiter
  1837. app_protocol = protocol = protocol_factory()
  1838. ssl_waiter = None
  1839. context = Context_CopyCurrent()
  1840. if ssl:
  1841. if server_hostname is None:
  1842. raise ValueError('You must set server_hostname '
  1843. 'when using ssl without a host')
  1844. ssl_waiter = self._new_future()
  1845. sslcontext = None if isinstance(ssl, bool) else ssl
  1846. protocol = SSLProtocol(
  1847. self, app_protocol, sslcontext, ssl_waiter,
  1848. False, server_hostname,
  1849. ssl_handshake_timeout=ssl_handshake_timeout,
  1850. ssl_shutdown_timeout=ssl_shutdown_timeout)
  1851. else:
  1852. if server_hostname is not None:
  1853. raise ValueError('server_hostname is only meaningful with ssl')
  1854. if ssl_handshake_timeout is not None:
  1855. raise ValueError(
  1856. 'ssl_handshake_timeout is only meaningful with ssl')
  1857. if ssl_shutdown_timeout is not None:
  1858. raise ValueError(
  1859. 'ssl_shutdown_timeout is only meaningful with ssl')
  1860. if path is not None:
  1861. if sock is not None:
  1862. raise ValueError(
  1863. 'path and sock can not be specified at the same time')
  1864. path = os_fspath(path)
  1865. if isinstance(path, str):
  1866. path = PyUnicode_EncodeFSDefault(path)
  1867. waiter = self._new_future()
  1868. tr = UnixTransport.new(self, protocol, None, waiter, context)
  1869. tr.connect(path)
  1870. try:
  1871. await waiter
  1872. except (KeyboardInterrupt, SystemExit):
  1873. raise
  1874. except BaseException:
  1875. tr._close()
  1876. raise
  1877. else:
  1878. if sock is None:
  1879. raise ValueError('no path and sock were specified')
  1880. if sock.family != uv.AF_UNIX or not _is_sock_stream(sock.type):
  1881. raise ValueError(
  1882. 'A UNIX Domain Stream Socket was expected, got {!r}'
  1883. .format(sock))
  1884. # libuv will set the socket to non-blocking mode, but
  1885. # we want Python socket object to notice that.
  1886. sock.setblocking(False)
  1887. waiter = self._new_future()
  1888. tr = UnixTransport.new(self, protocol, None, waiter, context)
  1889. try:
  1890. tr._open(sock.fileno())
  1891. tr._init_protocol()
  1892. await waiter
  1893. except (KeyboardInterrupt, SystemExit):
  1894. raise
  1895. except BaseException:
  1896. tr._close()
  1897. raise
  1898. tr._attach_fileobj(sock)
  1899. if ssl:
  1900. app_transport = protocol._get_app_transport(Context_CopyCurrent())
  1901. try:
  1902. await ssl_waiter
  1903. except (KeyboardInterrupt, SystemExit):
  1904. raise
  1905. except BaseException:
  1906. app_transport.close()
  1907. raise
  1908. return app_transport, app_protocol
  1909. else:
  1910. return tr, protocol
  1911. def default_exception_handler(self, context):
  1912. """Default exception handler.
  1913. This is called when an exception occurs and no exception
  1914. handler is set, and can be called by a custom exception
  1915. handler that wants to defer to the default behavior.
  1916. The context parameter has the same meaning as in
  1917. `call_exception_handler()`.
  1918. """
  1919. message = context.get('message')
  1920. if not message:
  1921. message = 'Unhandled exception in event loop'
  1922. exception = context.get('exception')
  1923. if exception is not None:
  1924. exc_info = (type(exception), exception, exception.__traceback__)
  1925. else:
  1926. exc_info = False
  1927. log_lines = [message]
  1928. for key in sorted(context):
  1929. if key in {'message', 'exception'}:
  1930. continue
  1931. value = context[key]
  1932. if key == 'source_traceback':
  1933. tb = ''.join(tb_format_list(value))
  1934. value = 'Object created at (most recent call last):\n'
  1935. value += tb.rstrip()
  1936. else:
  1937. try:
  1938. value = repr(value)
  1939. except (KeyboardInterrupt, SystemExit):
  1940. raise
  1941. except BaseException as ex:
  1942. value = ('Exception in __repr__ {!r}; '
  1943. 'value type: {!r}'.format(ex, type(value)))
  1944. log_lines.append('{}: {}'.format(key, value))
  1945. aio_logger.error('\n'.join(log_lines), exc_info=exc_info)
  1946. def get_exception_handler(self):
  1947. """Return an exception handler, or None if the default one is in use.
  1948. """
  1949. return self._exception_handler
  1950. def set_exception_handler(self, handler):
  1951. """Set handler as the new event loop exception handler.
  1952. If handler is None, the default exception handler will
  1953. be set.
  1954. If handler is a callable object, it should have a
  1955. signature matching '(loop, context)', where 'loop'
  1956. will be a reference to the active event loop, 'context'
  1957. will be a dict object (see `call_exception_handler()`
  1958. documentation for details about context).
  1959. """
  1960. if handler is not None and not callable(handler):
  1961. raise TypeError('A callable object or None is expected, '
  1962. 'got {!r}'.format(handler))
  1963. self._exception_handler = handler
  1964. def call_exception_handler(self, context):
  1965. """Call the current event loop's exception handler.
  1966. The context argument is a dict containing the following keys:
  1967. - 'message': Error message;
  1968. - 'exception' (optional): Exception object;
  1969. - 'future' (optional): Future instance;
  1970. - 'handle' (optional): Handle instance;
  1971. - 'protocol' (optional): Protocol instance;
  1972. - 'transport' (optional): Transport instance;
  1973. - 'socket' (optional): Socket instance.
  1974. New keys maybe introduced in the future.
  1975. Note: do not overload this method in an event loop subclass.
  1976. For custom exception handling, use the
  1977. `set_exception_handler()` method.
  1978. """
  1979. if UVLOOP_DEBUG:
  1980. self._debug_exception_handler_cnt += 1
  1981. if self._exception_handler is None:
  1982. try:
  1983. self.default_exception_handler(context)
  1984. except (KeyboardInterrupt, SystemExit):
  1985. raise
  1986. except BaseException:
  1987. # Second protection layer for unexpected errors
  1988. # in the default implementation, as well as for subclassed
  1989. # event loops with overloaded "default_exception_handler".
  1990. aio_logger.error('Exception in default exception handler',
  1991. exc_info=True)
  1992. else:
  1993. try:
  1994. self._exception_handler(self, context)
  1995. except (KeyboardInterrupt, SystemExit):
  1996. raise
  1997. except BaseException as exc:
  1998. # Exception in the user set custom exception handler.
  1999. try:
  2000. # Let's try default handler.
  2001. self.default_exception_handler({
  2002. 'message': 'Unhandled error in exception handler',
  2003. 'exception': exc,
  2004. 'context': context,
  2005. })
  2006. except (KeyboardInterrupt, SystemExit):
  2007. raise
  2008. except BaseException:
  2009. # Guard 'default_exception_handler' in case it is
  2010. # overloaded.
  2011. aio_logger.error('Exception in default exception handler '
  2012. 'while handling an unexpected error '
  2013. 'in custom exception handler',
  2014. exc_info=True)
  2015. def add_reader(self, fileobj, callback, *args):
  2016. """Add a reader callback."""
  2017. if len(args) == 0:
  2018. args = None
  2019. self._add_reader(fileobj, new_Handle(self, callback, args, None))
  2020. def remove_reader(self, fileobj):
  2021. """Remove a reader callback."""
  2022. self._remove_reader(fileobj)
  2023. def add_writer(self, fileobj, callback, *args):
  2024. """Add a writer callback.."""
  2025. if len(args) == 0:
  2026. args = None
  2027. self._add_writer(fileobj, new_Handle(self, callback, args, None))
  2028. def remove_writer(self, fileobj):
  2029. """Remove a writer callback."""
  2030. self._remove_writer(fileobj)
  2031. @cython.iterable_coroutine
  2032. async def sock_recv(self, sock, n):
  2033. """Receive data from the socket.
  2034. The return value is a bytes object representing the data received.
  2035. The maximum amount of data to be received at once is specified by
  2036. nbytes.
  2037. This method is a coroutine.
  2038. """
  2039. cdef:
  2040. Handle handle
  2041. if self._debug and sock.gettimeout() != 0:
  2042. raise ValueError("the socket must be non-blocking")
  2043. fut = _SyncSocketReaderFuture(sock, self)
  2044. handle = new_MethodHandle3(
  2045. self,
  2046. "Loop._sock_recv",
  2047. <method3_t>self._sock_recv,
  2048. None,
  2049. self,
  2050. fut, sock, n)
  2051. self._add_reader(sock, handle)
  2052. return await fut
  2053. @cython.iterable_coroutine
  2054. async def sock_recv_into(self, sock, buf):
  2055. """Receive data from the socket.
  2056. The received data is written into *buf* (a writable buffer).
  2057. The return value is the number of bytes written.
  2058. This method is a coroutine.
  2059. """
  2060. cdef:
  2061. Handle handle
  2062. if self._debug and sock.gettimeout() != 0:
  2063. raise ValueError("the socket must be non-blocking")
  2064. fut = _SyncSocketReaderFuture(sock, self)
  2065. handle = new_MethodHandle3(
  2066. self,
  2067. "Loop._sock_recv_into",
  2068. <method3_t>self._sock_recv_into,
  2069. None,
  2070. self,
  2071. fut, sock, buf)
  2072. self._add_reader(sock, handle)
  2073. return await fut
  2074. @cython.iterable_coroutine
  2075. async def sock_sendall(self, sock, data):
  2076. """Send data to the socket.
  2077. The socket must be connected to a remote socket. This method continues
  2078. to send data from data until either all data has been sent or an
  2079. error occurs. None is returned on success. On error, an exception is
  2080. raised, and there is no way to determine how much data, if any, was
  2081. successfully processed by the receiving end of the connection.
  2082. This method is a coroutine.
  2083. """
  2084. cdef:
  2085. Handle handle
  2086. ssize_t n
  2087. if self._debug and sock.gettimeout() != 0:
  2088. raise ValueError("the socket must be non-blocking")
  2089. if not data:
  2090. return
  2091. socket_inc_io_ref(sock)
  2092. try:
  2093. try:
  2094. n = sock.send(data)
  2095. except (BlockingIOError, InterruptedError):
  2096. pass
  2097. else:
  2098. if UVLOOP_DEBUG:
  2099. # This can be a partial success, i.e. only part
  2100. # of the data was sent
  2101. self._sock_try_write_total += 1
  2102. if n == len(data):
  2103. return
  2104. if not isinstance(data, memoryview):
  2105. data = memoryview(data)
  2106. data = data[n:]
  2107. fut = _SyncSocketWriterFuture(sock, self)
  2108. handle = new_MethodHandle3(
  2109. self,
  2110. "Loop._sock_sendall",
  2111. <method3_t>self._sock_sendall,
  2112. None,
  2113. self,
  2114. fut, sock, data)
  2115. self._add_writer(sock, handle)
  2116. return await fut
  2117. finally:
  2118. socket_dec_io_ref(sock)
  2119. @cython.iterable_coroutine
  2120. async def sock_accept(self, sock):
  2121. """Accept a connection.
  2122. The socket must be bound to an address and listening for connections.
  2123. The return value is a pair (conn, address) where conn is a new socket
  2124. object usable to send and receive data on the connection, and address
  2125. is the address bound to the socket on the other end of the connection.
  2126. This method is a coroutine.
  2127. """
  2128. cdef:
  2129. Handle handle
  2130. if self._debug and sock.gettimeout() != 0:
  2131. raise ValueError("the socket must be non-blocking")
  2132. fut = _SyncSocketReaderFuture(sock, self)
  2133. handle = new_MethodHandle2(
  2134. self,
  2135. "Loop._sock_accept",
  2136. <method2_t>self._sock_accept,
  2137. None,
  2138. self,
  2139. fut, sock)
  2140. self._add_reader(sock, handle)
  2141. return await fut
  2142. @cython.iterable_coroutine
  2143. async def sock_connect(self, sock, address):
  2144. """Connect to a remote socket at address.
  2145. This method is a coroutine.
  2146. """
  2147. if self._debug and sock.gettimeout() != 0:
  2148. raise ValueError("the socket must be non-blocking")
  2149. socket_inc_io_ref(sock)
  2150. try:
  2151. if sock.family == uv.AF_UNIX:
  2152. fut = self._sock_connect(sock, address)
  2153. else:
  2154. addrs = await self.getaddrinfo(
  2155. *address[:2], family=sock.family)
  2156. _, _, _, _, address = addrs[0]
  2157. fut = self._sock_connect(sock, address)
  2158. if fut is not None:
  2159. await fut
  2160. finally:
  2161. socket_dec_io_ref(sock)
  2162. @cython.iterable_coroutine
  2163. async def sock_recvfrom(self, sock, bufsize):
  2164. raise NotImplementedError
  2165. @cython.iterable_coroutine
  2166. async def sock_recvfrom_into(self, sock, buf, nbytes=0):
  2167. raise NotImplementedError
  2168. @cython.iterable_coroutine
  2169. async def sock_sendto(self, sock, data, address):
  2170. raise NotImplementedError
  2171. @cython.iterable_coroutine
  2172. async def connect_accepted_socket(self, protocol_factory, sock, *,
  2173. ssl=None,
  2174. ssl_handshake_timeout=None,
  2175. ssl_shutdown_timeout=None):
  2176. """Handle an accepted connection.
  2177. This is used by servers that accept connections outside of
  2178. asyncio but that use asyncio to handle connections.
  2179. This method is a coroutine. When completed, the coroutine
  2180. returns a (transport, protocol) pair.
  2181. """
  2182. cdef:
  2183. UVStream transport = None
  2184. if ssl is not None:
  2185. if not isinstance(ssl, ssl_SSLContext):
  2186. raise TypeError('ssl argument must be an SSLContext or None')
  2187. else:
  2188. if ssl_handshake_timeout is not None:
  2189. raise ValueError(
  2190. 'ssl_handshake_timeout is only meaningful with ssl')
  2191. if ssl_shutdown_timeout is not None:
  2192. raise ValueError(
  2193. 'ssl_shutdown_timeout is only meaningful with ssl')
  2194. if not _is_sock_stream(sock.type):
  2195. raise ValueError(
  2196. 'A Stream Socket was expected, got {!r}'.format(sock))
  2197. app_protocol = protocol_factory()
  2198. waiter = self._new_future()
  2199. transport_waiter = None
  2200. context = Context_CopyCurrent()
  2201. if ssl is None:
  2202. protocol = app_protocol
  2203. transport_waiter = waiter
  2204. else:
  2205. protocol = SSLProtocol(
  2206. self, app_protocol, ssl, waiter,
  2207. server_side=True,
  2208. server_hostname=None,
  2209. ssl_handshake_timeout=ssl_handshake_timeout,
  2210. ssl_shutdown_timeout=ssl_shutdown_timeout)
  2211. transport_waiter = None
  2212. if sock.family == uv.AF_UNIX:
  2213. transport = <UVStream>UnixTransport.new(
  2214. self, protocol, None, transport_waiter, context)
  2215. elif sock.family in (uv.AF_INET, uv.AF_INET6):
  2216. transport = <UVStream>TCPTransport.new(
  2217. self, protocol, None, transport_waiter, context)
  2218. if transport is None:
  2219. raise ValueError(
  2220. 'invalid socket family, expected AF_UNIX, AF_INET or AF_INET6')
  2221. transport._open(sock.fileno())
  2222. transport._init_protocol()
  2223. transport._attach_fileobj(sock)
  2224. if ssl:
  2225. app_transport = protocol._get_app_transport(context)
  2226. try:
  2227. await waiter
  2228. except (KeyboardInterrupt, SystemExit):
  2229. raise
  2230. except BaseException:
  2231. app_transport.close()
  2232. raise
  2233. return app_transport, protocol
  2234. else:
  2235. try:
  2236. await waiter
  2237. except (KeyboardInterrupt, SystemExit):
  2238. raise
  2239. except BaseException:
  2240. transport._close()
  2241. raise
  2242. return transport, protocol
  2243. def run_in_executor(self, executor, func, *args):
  2244. if aio_iscoroutine(func) or aio_iscoroutinefunction(func):
  2245. raise TypeError("coroutines cannot be used with run_in_executor()")
  2246. self._check_closed()
  2247. if executor is None:
  2248. executor = self._default_executor
  2249. # Only check when the default executor is being used
  2250. self._check_default_executor()
  2251. if executor is None:
  2252. executor = cc_ThreadPoolExecutor()
  2253. self._default_executor = executor
  2254. return aio_wrap_future(executor.submit(func, *args), loop=self)
  2255. def set_default_executor(self, executor):
  2256. self._default_executor = executor
  2257. @cython.iterable_coroutine
  2258. async def __subprocess_run(self, protocol_factory, args,
  2259. stdin=subprocess_PIPE,
  2260. stdout=subprocess_PIPE,
  2261. stderr=subprocess_PIPE,
  2262. universal_newlines=False,
  2263. shell=True,
  2264. bufsize=0,
  2265. preexec_fn=None,
  2266. close_fds=None,
  2267. cwd=None,
  2268. env=None,
  2269. startupinfo=None,
  2270. creationflags=0,
  2271. restore_signals=True,
  2272. start_new_session=False,
  2273. executable=None,
  2274. pass_fds=(),
  2275. **kwargs):
  2276. # TODO: Implement close_fds (might not be very important in
  2277. # Python 3.5, since all FDs aren't inheritable by default.)
  2278. cdef:
  2279. int debug_flags = 0
  2280. if universal_newlines:
  2281. raise ValueError("universal_newlines must be False")
  2282. if bufsize != 0:
  2283. raise ValueError("bufsize must be 0")
  2284. if startupinfo is not None:
  2285. raise ValueError('startupinfo is not supported')
  2286. if creationflags != 0:
  2287. raise ValueError('creationflags is not supported')
  2288. if executable is not None:
  2289. args[0] = executable
  2290. # For tests only! Do not use in your code. Ever.
  2291. if kwargs.pop("__uvloop_sleep_after_fork", False):
  2292. debug_flags |= __PROCESS_DEBUG_SLEEP_AFTER_FORK
  2293. if kwargs:
  2294. raise ValueError(
  2295. 'unexpected kwargs: {}'.format(', '.join(kwargs.keys())))
  2296. waiter = self._new_future()
  2297. protocol = protocol_factory()
  2298. proc = UVProcessTransport.new(self, protocol,
  2299. args, env, cwd, start_new_session,
  2300. stdin, stdout, stderr, pass_fds,
  2301. waiter,
  2302. debug_flags,
  2303. preexec_fn,
  2304. restore_signals)
  2305. try:
  2306. await waiter
  2307. except (KeyboardInterrupt, SystemExit):
  2308. raise
  2309. except BaseException:
  2310. proc.close()
  2311. raise
  2312. return proc, protocol
  2313. @cython.iterable_coroutine
  2314. async def subprocess_shell(self, protocol_factory, cmd, *,
  2315. shell=True,
  2316. **kwargs):
  2317. if not shell:
  2318. raise ValueError("shell must be True")
  2319. args = [cmd]
  2320. if shell:
  2321. args = [b'/bin/sh', b'-c'] + args
  2322. return await self.__subprocess_run(protocol_factory, args, shell=True,
  2323. **kwargs)
  2324. @cython.iterable_coroutine
  2325. async def subprocess_exec(self, protocol_factory, program, *args,
  2326. shell=False, **kwargs):
  2327. if shell:
  2328. raise ValueError("shell must be False")
  2329. args = list((program,) + args)
  2330. return await self.__subprocess_run(protocol_factory, args, shell=False,
  2331. **kwargs)
  2332. @cython.iterable_coroutine
  2333. async def connect_read_pipe(self, proto_factory, pipe):
  2334. """Register read pipe in event loop. Set the pipe to non-blocking mode.
  2335. protocol_factory should instantiate object with Protocol interface.
  2336. pipe is a file-like object.
  2337. Return pair (transport, protocol), where transport supports the
  2338. ReadTransport interface."""
  2339. cdef:
  2340. ReadUnixTransport transp
  2341. waiter = self._new_future()
  2342. proto = proto_factory()
  2343. transp = ReadUnixTransport.new(self, proto, None, waiter)
  2344. transp._add_extra_info('pipe', pipe)
  2345. try:
  2346. transp._open(pipe.fileno())
  2347. transp._init_protocol()
  2348. await waiter
  2349. except (KeyboardInterrupt, SystemExit):
  2350. raise
  2351. except BaseException:
  2352. transp._close()
  2353. raise
  2354. transp._attach_fileobj(pipe)
  2355. return transp, proto
  2356. @cython.iterable_coroutine
  2357. async def connect_write_pipe(self, proto_factory, pipe):
  2358. """Register write pipe in event loop.
  2359. protocol_factory should instantiate object with BaseProtocol interface.
  2360. Pipe is file-like object already switched to nonblocking.
  2361. Return pair (transport, protocol), where transport support
  2362. WriteTransport interface."""
  2363. cdef:
  2364. WriteUnixTransport transp
  2365. waiter = self._new_future()
  2366. proto = proto_factory()
  2367. transp = WriteUnixTransport.new(self, proto, None, waiter)
  2368. transp._add_extra_info('pipe', pipe)
  2369. try:
  2370. transp._open(pipe.fileno())
  2371. transp._init_protocol()
  2372. await waiter
  2373. except (KeyboardInterrupt, SystemExit):
  2374. raise
  2375. except BaseException:
  2376. transp._close()
  2377. raise
  2378. transp._attach_fileobj(pipe)
  2379. return transp, proto
  2380. def add_signal_handler(self, sig, callback, *args):
  2381. """Add a handler for a signal. UNIX only.
  2382. Raise ValueError if the signal number is invalid or uncatchable.
  2383. Raise RuntimeError if there is a problem setting up the handler.
  2384. """
  2385. cdef:
  2386. Handle h
  2387. if not self._is_main_thread():
  2388. raise ValueError(
  2389. 'add_signal_handler() can only be called from '
  2390. 'the main thread')
  2391. if (aio_iscoroutine(callback)
  2392. or aio_iscoroutinefunction(callback)):
  2393. raise TypeError(
  2394. "coroutines cannot be used with add_signal_handler()")
  2395. if sig == uv.SIGCHLD:
  2396. if (hasattr(callback, '__self__') and
  2397. isinstance(callback.__self__, aio_AbstractChildWatcher)):
  2398. warnings_warn(
  2399. "!!! asyncio is trying to install its ChildWatcher for "
  2400. "SIGCHLD signal !!!\n\nThis is probably because a uvloop "
  2401. "instance is used with asyncio.set_event_loop(). "
  2402. "The correct way to use uvloop is to install its policy: "
  2403. "`asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())`"
  2404. "\n\n", RuntimeWarning, source=self)
  2405. # TODO: ideally we should always raise an error here,
  2406. # but that would be a backwards incompatible change,
  2407. # because we recommended using "asyncio.set_event_loop()"
  2408. # in our README. Need to start a deprecation period
  2409. # at some point to turn this warning into an error.
  2410. return
  2411. raise RuntimeError(
  2412. 'cannot add a signal handler for SIGCHLD: it is used '
  2413. 'by the event loop to track subprocesses')
  2414. self._check_signal(sig)
  2415. self._check_closed()
  2416. h = new_Handle(self, callback, args or None, None)
  2417. self._signal_handlers[sig] = h
  2418. try:
  2419. # Register a dummy signal handler to ask Python to write the signal
  2420. # number in the wakeup file descriptor.
  2421. signal_signal(sig, self.__sighandler)
  2422. # Set SA_RESTART to limit EINTR occurrences.
  2423. signal_siginterrupt(sig, False)
  2424. except OSError as exc:
  2425. del self._signal_handlers[sig]
  2426. if not self._signal_handlers:
  2427. try:
  2428. signal_set_wakeup_fd(-1)
  2429. except (ValueError, OSError) as nexc:
  2430. aio_logger.info('set_wakeup_fd(-1) failed: %s', nexc)
  2431. if exc.errno == errno_EINVAL:
  2432. raise RuntimeError('sig {} cannot be caught'.format(sig))
  2433. else:
  2434. raise
  2435. def remove_signal_handler(self, sig):
  2436. """Remove a handler for a signal. UNIX only.
  2437. Return True if a signal handler was removed, False if not.
  2438. """
  2439. if not self._is_main_thread():
  2440. raise ValueError(
  2441. 'remove_signal_handler() can only be called from '
  2442. 'the main thread')
  2443. self._check_signal(sig)
  2444. if not self._listening_signals:
  2445. return False
  2446. try:
  2447. del self._signal_handlers[sig]
  2448. except KeyError:
  2449. return False
  2450. if sig == uv.SIGINT:
  2451. handler = signal_default_int_handler
  2452. else:
  2453. handler = signal_SIG_DFL
  2454. try:
  2455. signal_signal(sig, handler)
  2456. except OSError as exc:
  2457. if exc.errno == errno_EINVAL:
  2458. raise RuntimeError('sig {} cannot be caught'.format(sig))
  2459. else:
  2460. raise
  2461. return True
  2462. @cython.iterable_coroutine
  2463. async def create_datagram_endpoint(self, protocol_factory,
  2464. local_addr=None, remote_addr=None, *,
  2465. family=0, proto=0, flags=0,
  2466. reuse_address=_unset, reuse_port=None,
  2467. allow_broadcast=None, sock=None):
  2468. """A coroutine which creates a datagram endpoint.
  2469. This method will try to establish the endpoint in the background.
  2470. When successful, the coroutine returns a (transport, protocol) pair.
  2471. protocol_factory must be a callable returning a protocol instance.
  2472. socket family AF_INET or socket.AF_INET6 depending on host (or
  2473. family if specified), socket type SOCK_DGRAM.
  2474. reuse_port tells the kernel to allow this endpoint to be bound to
  2475. the same port as other existing endpoints are bound to, so long as
  2476. they all set this flag when being created. This option is not
  2477. supported on Windows and some UNIX's. If the
  2478. :py:data:`~socket.SO_REUSEPORT` constant is not defined then this
  2479. capability is unsupported.
  2480. allow_broadcast tells the kernel to allow this endpoint to send
  2481. messages to the broadcast address.
  2482. sock can optionally be specified in order to use a preexisting
  2483. socket object.
  2484. """
  2485. cdef:
  2486. UDPTransport udp = None
  2487. system.addrinfo * lai
  2488. system.addrinfo * rai
  2489. if sock is not None:
  2490. if not _is_sock_dgram(sock.type):
  2491. raise ValueError(
  2492. 'A UDP Socket was expected, got {!r}'.format(sock))
  2493. if (local_addr or remote_addr or
  2494. family or proto or flags or
  2495. reuse_port or allow_broadcast):
  2496. # show the problematic kwargs in exception msg
  2497. opts = dict(local_addr=local_addr, remote_addr=remote_addr,
  2498. family=family, proto=proto, flags=flags,
  2499. reuse_address=reuse_address, reuse_port=reuse_port,
  2500. allow_broadcast=allow_broadcast)
  2501. problems = ', '.join(
  2502. '{}={}'.format(k, v) for k, v in opts.items() if v)
  2503. raise ValueError(
  2504. 'socket modifier keyword arguments can not be used '
  2505. 'when sock is specified. ({})'.format(problems))
  2506. sock.setblocking(False)
  2507. udp = UDPTransport.__new__(UDPTransport)
  2508. udp._init(self, uv.AF_UNSPEC)
  2509. udp.open(sock.family, sock.fileno())
  2510. udp._attach_fileobj(sock)
  2511. else:
  2512. if reuse_address is not _unset:
  2513. if reuse_address:
  2514. raise ValueError("Passing `reuse_address=True` is no "
  2515. "longer supported, as the usage of "
  2516. "SO_REUSEPORT in UDP poses a significant "
  2517. "security concern.")
  2518. else:
  2519. warnings_warn("The *reuse_address* parameter has been "
  2520. "deprecated as of 0.15.", DeprecationWarning,
  2521. stacklevel=2)
  2522. reuse_port = bool(reuse_port)
  2523. if reuse_port and not has_SO_REUSEPORT:
  2524. raise ValueError(
  2525. 'reuse_port not supported by socket module')
  2526. lads = None
  2527. if local_addr is not None:
  2528. if (not isinstance(local_addr, (tuple, list)) or
  2529. len(local_addr) != 2):
  2530. raise TypeError(
  2531. 'local_addr must be a tuple of (host, port)')
  2532. lads = await self._getaddrinfo(
  2533. local_addr[0], local_addr[1],
  2534. family, uv.SOCK_DGRAM, proto, flags,
  2535. 0)
  2536. rads = None
  2537. if remote_addr is not None:
  2538. if (not isinstance(remote_addr, (tuple, list)) or
  2539. len(remote_addr) != 2):
  2540. raise TypeError(
  2541. 'remote_addr must be a tuple of (host, port)')
  2542. rads = await self._getaddrinfo(
  2543. remote_addr[0], remote_addr[1],
  2544. family, uv.SOCK_DGRAM, proto, flags,
  2545. 0)
  2546. excs = []
  2547. if lads is None:
  2548. if rads is not None:
  2549. udp = UDPTransport.__new__(UDPTransport)
  2550. rai = (<AddrInfo>rads).data
  2551. udp._init(self, rai.ai_family)
  2552. udp._connect(rai.ai_addr, rai.ai_addrlen)
  2553. udp._set_address(rai)
  2554. else:
  2555. if family not in (uv.AF_INET, uv.AF_INET6):
  2556. raise ValueError('unexpected address family')
  2557. udp = UDPTransport.__new__(UDPTransport)
  2558. udp._init(self, family)
  2559. if reuse_port:
  2560. self._sock_set_reuseport(udp._fileno())
  2561. else:
  2562. lai = (<AddrInfo>lads).data
  2563. while lai is not NULL:
  2564. try:
  2565. udp = UDPTransport.__new__(UDPTransport)
  2566. udp._init(self, lai.ai_family)
  2567. if reuse_port:
  2568. self._sock_set_reuseport(udp._fileno())
  2569. udp._bind(lai.ai_addr)
  2570. except (KeyboardInterrupt, SystemExit):
  2571. raise
  2572. except BaseException as ex:
  2573. lai = lai.ai_next
  2574. excs.append(ex)
  2575. continue
  2576. else:
  2577. break
  2578. else:
  2579. ctx = None
  2580. if len(excs):
  2581. ctx = excs[0]
  2582. raise OSError('could not bind to local_addr {}'.format(
  2583. local_addr)) from ctx
  2584. if rads is not None:
  2585. rai = (<AddrInfo>rads).data
  2586. while rai is not NULL:
  2587. if rai.ai_family != lai.ai_family:
  2588. rai = rai.ai_next
  2589. continue
  2590. if rai.ai_protocol != lai.ai_protocol:
  2591. rai = rai.ai_next
  2592. continue
  2593. udp._connect(rai.ai_addr, rai.ai_addrlen)
  2594. udp._set_address(rai)
  2595. break
  2596. else:
  2597. raise OSError(
  2598. 'could not bind to remote_addr {}'.format(
  2599. remote_addr))
  2600. if allow_broadcast:
  2601. udp._set_broadcast(1)
  2602. protocol = protocol_factory()
  2603. waiter = self._new_future()
  2604. assert udp is not None
  2605. udp._set_protocol(protocol)
  2606. udp._set_waiter(waiter)
  2607. udp._init_protocol()
  2608. await waiter
  2609. return udp, protocol
  2610. def _monitor_fs(self, path: str, callback) -> asyncio.Handle:
  2611. cdef:
  2612. UVFSEvent fs_handle
  2613. char* c_str_path
  2614. self._check_closed()
  2615. fs_handle = UVFSEvent.new(self, callback, None)
  2616. p_bytes = path.encode('UTF-8')
  2617. c_str_path = p_bytes
  2618. flags = 0
  2619. fs_handle.start(c_str_path, flags)
  2620. return fs_handle
  2621. def _check_default_executor(self):
  2622. if self._executor_shutdown_called:
  2623. raise RuntimeError('Executor shutdown has been called')
  2624. def _asyncgen_finalizer_hook(self, agen):
  2625. self._asyncgens.discard(agen)
  2626. if not self.is_closed():
  2627. self.call_soon_threadsafe(self.create_task, agen.aclose())
  2628. def _asyncgen_firstiter_hook(self, agen):
  2629. if self._asyncgens_shutdown_called:
  2630. warnings_warn(
  2631. "asynchronous generator {!r} was scheduled after "
  2632. "loop.shutdown_asyncgens() call".format(agen),
  2633. ResourceWarning, source=self)
  2634. self._asyncgens.add(agen)
  2635. @cython.iterable_coroutine
  2636. async def shutdown_asyncgens(self):
  2637. """Shutdown all active asynchronous generators."""
  2638. self._asyncgens_shutdown_called = True
  2639. if not len(self._asyncgens):
  2640. return
  2641. closing_agens = list(self._asyncgens)
  2642. self._asyncgens.clear()
  2643. shutdown_coro = aio_gather(
  2644. *[ag.aclose() for ag in closing_agens],
  2645. return_exceptions=True)
  2646. results = await shutdown_coro
  2647. for result, agen in zip(results, closing_agens):
  2648. if isinstance(result, Exception):
  2649. self.call_exception_handler({
  2650. 'message': 'an error occurred during closing of '
  2651. 'asynchronous generator {!r}'.format(agen),
  2652. 'exception': result,
  2653. 'asyncgen': agen
  2654. })
  2655. @cython.iterable_coroutine
  2656. async def shutdown_default_executor(self, timeout=None):
  2657. """Schedule the shutdown of the default executor.
  2658. The timeout parameter specifies the amount of time the executor will
  2659. be given to finish joining. The default value is None, which means
  2660. that the executor will be given an unlimited amount of time.
  2661. """
  2662. self._executor_shutdown_called = True
  2663. if self._default_executor is None:
  2664. return
  2665. future = self.create_future()
  2666. thread = threading_Thread(target=self._do_shutdown, args=(future,))
  2667. thread.start()
  2668. try:
  2669. await future
  2670. finally:
  2671. thread.join(timeout)
  2672. if thread.is_alive():
  2673. warnings_warn(
  2674. "The executor did not finishing joining "
  2675. f"its threads within {timeout} seconds.",
  2676. RuntimeWarning,
  2677. stacklevel=2
  2678. )
  2679. self._default_executor.shutdown(wait=False)
  2680. def _do_shutdown(self, future):
  2681. try:
  2682. self._default_executor.shutdown(wait=True)
  2683. self.call_soon_threadsafe(future.set_result, None)
  2684. except Exception as ex:
  2685. self.call_soon_threadsafe(future.set_exception, ex)
  2686. # Expose pointer for integration with other C-extensions
  2687. def libuv_get_loop_t_ptr(loop):
  2688. return PyCapsule_New(<void *>(<Loop>loop).uvloop, NULL, NULL)
  2689. def libuv_get_version():
  2690. return uv.uv_version()
  2691. def _testhelper_unwrap_capsuled_pointer(obj):
  2692. return <uint64_t>PyCapsule_GetPointer(obj, NULL)
  2693. cdef void __loop_alloc_buffer(
  2694. uv.uv_handle_t* uvhandle,
  2695. size_t suggested_size,
  2696. uv.uv_buf_t* buf
  2697. ) noexcept with gil:
  2698. cdef:
  2699. Loop loop = (<UVHandle>uvhandle.data)._loop
  2700. if loop._recv_buffer_in_use == 1:
  2701. buf.len = 0
  2702. exc = RuntimeError('concurrent allocations')
  2703. loop._handle_exception(exc)
  2704. return
  2705. loop._recv_buffer_in_use = 1
  2706. buf.base = loop._recv_buffer
  2707. buf.len = sizeof(loop._recv_buffer)
  2708. cdef inline void __loop_free_buffer(Loop loop):
  2709. loop._recv_buffer_in_use = 0
  2710. class _SyncSocketReaderFuture(aio_Future):
  2711. def __init__(self, sock, loop):
  2712. aio_Future.__init__(self, loop=loop)
  2713. self.__sock = sock
  2714. self.__loop = loop
  2715. def __remove_reader(self):
  2716. if self.__sock is not None and self.__sock.fileno() != -1:
  2717. self.__loop.remove_reader(self.__sock)
  2718. self.__sock = None
  2719. if PY39:
  2720. def cancel(self, msg=None):
  2721. self.__remove_reader()
  2722. aio_Future.cancel(self, msg=msg)
  2723. else:
  2724. def cancel(self):
  2725. self.__remove_reader()
  2726. aio_Future.cancel(self)
  2727. class _SyncSocketWriterFuture(aio_Future):
  2728. def __init__(self, sock, loop):
  2729. aio_Future.__init__(self, loop=loop)
  2730. self.__sock = sock
  2731. self.__loop = loop
  2732. def __remove_writer(self):
  2733. if self.__sock is not None and self.__sock.fileno() != -1:
  2734. self.__loop.remove_writer(self.__sock)
  2735. self.__sock = None
  2736. if PY39:
  2737. def cancel(self, msg=None):
  2738. self.__remove_writer()
  2739. aio_Future.cancel(self, msg=msg)
  2740. else:
  2741. def cancel(self):
  2742. self.__remove_writer()
  2743. aio_Future.cancel(self)
  2744. include "cbhandles.pyx"
  2745. include "pseudosock.pyx"
  2746. include "lru.pyx"
  2747. include "handles/handle.pyx"
  2748. include "handles/async_.pyx"
  2749. include "handles/idle.pyx"
  2750. include "handles/check.pyx"
  2751. include "handles/timer.pyx"
  2752. include "handles/poll.pyx"
  2753. include "handles/basetransport.pyx"
  2754. include "handles/stream.pyx"
  2755. include "handles/streamserver.pyx"
  2756. include "handles/tcp.pyx"
  2757. include "handles/pipe.pyx"
  2758. include "handles/process.pyx"
  2759. include "handles/fsevent.pyx"
  2760. include "request.pyx"
  2761. include "dns.pyx"
  2762. include "sslproto.pyx"
  2763. include "handles/udp.pyx"
  2764. include "server.pyx"
  2765. # Used in UVProcess
  2766. cdef vint __atfork_installed = 0
  2767. cdef vint __forking = 0
  2768. cdef Loop __forking_loop = None
  2769. cdef void __get_fork_handler() noexcept nogil:
  2770. with gil:
  2771. if (__forking and __forking_loop is not None and
  2772. __forking_loop.active_process_handler is not None):
  2773. __forking_loop.active_process_handler._after_fork()
  2774. cdef __install_atfork():
  2775. global __atfork_installed
  2776. if __atfork_installed:
  2777. return
  2778. __atfork_installed = 1
  2779. cdef int err
  2780. err = system.pthread_atfork(NULL, NULL, &system.handleAtFork)
  2781. if err:
  2782. __atfork_installed = 0
  2783. raise convert_error(-err)
  2784. # Install PyMem* memory allocators
  2785. cdef vint __mem_installed = 0
  2786. cdef __install_pymem():
  2787. global __mem_installed
  2788. if __mem_installed:
  2789. return
  2790. __mem_installed = 1
  2791. cdef int err
  2792. err = uv.uv_replace_allocator(<uv.uv_malloc_func>PyMem_RawMalloc,
  2793. <uv.uv_realloc_func>PyMem_RawRealloc,
  2794. <uv.uv_calloc_func>PyMem_RawCalloc,
  2795. <uv.uv_free_func>PyMem_RawFree)
  2796. if err < 0:
  2797. __mem_installed = 0
  2798. raise convert_error(err)
  2799. cdef _set_signal_wakeup_fd(fd):
  2800. if fd >= 0:
  2801. return signal_set_wakeup_fd(fd, warn_on_full_buffer=False)
  2802. else:
  2803. return signal_set_wakeup_fd(fd)
  2804. # Helpers for tests
  2805. @cython.iterable_coroutine
  2806. async def _test_coroutine_1():
  2807. return 42