@@ -141,33 +141,40 @@ class Ares {
141141 name: String ,
142142 replyParser: ReplyParser
143143 ) async throws -> ReplyParser . Reply {
144- try await withCheckedThrowingContinuation { continuation in
145- let handler = QueryReplyHandler ( parser: replyParser, continuation)
144+ try await withTaskCancellationHandler (
145+ operation: {
146+ try await withCheckedThrowingContinuation { continuation in
147+ let handler = QueryReplyHandler ( parser: replyParser, continuation)
148+
149+ // Wrap `handler` into a pointer so we can pass it to callback. The pointer will be deallocated in there later.
150+ let handlerPointer = UnsafeMutableRawPointer . allocate (
151+ byteCount: MemoryLayout< QueryReplyHandler> . stride,
152+ alignment: MemoryLayout< QueryReplyHandler> . alignment
153+ )
154+ handlerPointer. initializeMemory ( as: QueryReplyHandler . self, repeating: handler, count: 1 )
146155
147- // Wrap `handler` into a pointer so we can pass it to callback. The pointer will be deallocated in there later.
148- let handlerPointer = UnsafeMutableRawPointer . allocate (
149- byteCount: MemoryLayout< QueryReplyHandler> . stride,
150- alignment: MemoryLayout< QueryReplyHandler> . alignment
151- )
152- handlerPointer. initializeMemory ( as: QueryReplyHandler . self, repeating: handler, count: 1 )
156+ let queryCallback : QueryCallback = { arg, status, _, buf, len in
157+ guard let handlerPointer = arg else {
158+ preconditionFailure ( " 'arg' is nil. This is a bug. " )
159+ }
153160
154- let queryCallback : QueryCallback = { arg, status, _, buf, len in
155- guard let handlerPointer = arg else {
156- preconditionFailure ( " 'arg' is nil. This is a bug. " )
157- }
161+ let handler = QueryReplyHandler ( pointer: handlerPointer)
162+ defer { handlerPointer. deallocate ( ) }
158163
159- let handler = QueryReplyHandler ( pointer: handlerPointer)
160- defer { handlerPointer. deallocate ( ) }
161-
162- handler. handle ( status: status, buffer: buf, length: len)
163- }
164+ handler. handle ( status: status, buffer: buf, length: len)
165+ }
164166
165- Task {
166- await self . channel. withChannel { channel in
167- ares_query ( channel, name, DNSClass . IN. rawValue, type. intValue, queryCallback, handlerPointer)
167+ self . channel. withChannel { channel in
168+ ares_query ( channel, name, DNSClass . IN. rawValue, type. intValue, queryCallback, handlerPointer)
169+ }
170+ }
171+ } ,
172+ onCancel: {
173+ self . channel. withChannel { channel in
174+ ares_cancel ( channel)
168175 }
169176 }
170- }
177+ )
171178 }
172179
173180 /// See `arpa/nameser.h`.
@@ -199,7 +206,7 @@ extension Ares {
199206 func poll( ) async {
200207 var socks = [ ares_socket_t] ( repeating: ares_socket_t ( ) , count: Int ( ARES_GETSOCK_MAXNUM) )
201208
202- await self . channel. withChannel { channel in
209+ self . channel. withChannel { channel in
203210 // Indicates what actions (i.e., read/write) to wait for on the different sockets
204211 let bitmask = UInt32 ( ares_getsock ( channel, & socks, ARES_GETSOCK_MAXNUM) )
205212
0 commit comments