loop.pyx 117 KB

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