You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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