def _noConnection(self, failAfterFailures=None): """ Notify the caller that no connection is expected. @return: L{Deferred} that is fired with L{CancelledError}. """ return fail(CancelledError())
def add_collector_timeout(deferred, seconds): """Raise error on deferred when modeler is timed out.""" error = CancelledError("WBEM query timeout") def handle_timeout(): deferred.cancel() def handle_result(result): if timeout_d.active(): timeout_d.cancel() if isinstance(result, list): for item in result: if not item[0] and item[1].check(CancelledError): raise error return result def handle_failure(failure): # define this method to catch errors from canceled deferredlist # on Zenoss 4.2.x with Twisted v.11 if failure.check(CancelledError): raise error return failure timeout_d = reactor.callLater(seconds, handle_timeout) deferred.addBoth(handle_result) deferred.addErrback(handle_failure) return deferred
def handle_cancel(new_deferred: "defer.Deferred[T]") -> None: # before the new deferred is cancelled, we `pause` it to stop the cancellation # propagating. we then `unpause` it once the wrapped deferred completes, to # propagate the exception. new_deferred.pause() new_deferred.errback(Failure(CancelledError())) deferred.addBoth(lambda _: new_deferred.unpause())
class FakeServer(object): """ In-memory implementation of L{IStreamServerEndpoint}. @ivar result: The L{Deferred} resulting from the call to C{listen}, after C{listen} has been called. @ivar factory: The factory passed to C{listen}. @ivar cancelException: The exception to errback C{self.result} when it is cancelled. @ivar port: The L{IListeningPort} which C{listen}'s L{Deferred} will fire with. @ivar listenAttempts: The number of times C{listen} has been invoked. @ivar failImmediately: If set, the exception to fail the L{Deferred} returned from C{listen} before it is returned. """ result = None factory = None failImmediately = None cancelException = CancelledError() listenAttempts = 0 def __init__(self): self.port = FakePort() def listen(self, factory): """ Return a Deferred and store it for future use. (Implementation of L{IStreamServerEndpoint}). """ self.listenAttempts += 1 self.factory = factory self.result = Deferred( canceller=lambda d: d.errback(self.cancelException)) if self.failImmediately is not None: self.result.errback(self.failImmediately) return self.result def startedListening(self): """ Test code should invoke this method after causing C{listen} to be invoked in order to fire the L{Deferred} previously returned from C{listen}. """ self.result.callback(self.port) def stoppedListening(self): """ Test code should invoke this method after causing C{stopListening} to be invoked on the port fired from the L{Deferred} returned from C{listen} in order to cause the L{Deferred} returned from C{stopListening} to fire. """ self.port.deferred.callback(None)
def testDelayedDisconnectDoesNotFinishRequest(self): """ A C{CancelledError} exception is raised if content cannot be read from the request midway through processing, due to the client disconnecting. In such cases, the C{Request.finish} method is not invoked by the L{handleRequestError} handler to avoid causing a failure in Twisted. """ failure = Failure(CancelledError("Client disconnected partway.")) handleRequestError(failure, self.request, self.resource) self.assertFalse(self.request.finished)
def _cancel(self, d): """ Cancel the traversal with a CancelledError. """ if self._finished: return try: raise CancelledError() except: self._caught_failure = failure.Failure() self._iterate()
def _render_POST_thread(self, tx: Transaction, request: Request) -> Transaction: """ Method called in a thread to solve tx pow without stratum """ # TODO Tx should be resolved in the frontend def _should_stop(): return request.should_stop_mining_thread tx.start_mining(sleep_seconds=self.sleep_seconds, should_stop=_should_stop) if request.should_stop_mining_thread: raise CancelledError() tx.update_hash() tx.verify() return tx
def test_propagates_cancelled_error(self): """Test that a `CancelledError` from the original `Deferred` gets propagated.""" deferred: "Deferred[str]" = Deferred() wrapper_deferred = delay_cancellation(deferred) # Fail the original `Deferred` with a `CancelledError`. cancelled_error = CancelledError() deferred.errback(cancelled_error) # The new `Deferred` should fail with exactly the same `CancelledError`. self.assertTrue(wrapper_deferred.called) self.assertIs(cancelled_error, self.failureResultOf(wrapper_deferred).value)
def test_rpc_info_when_rpc_advertise_not_fully_started(self): self.useFixture(RegionEventLoopFixture("rpc", "rpc-advertise")) # Simulate a time-out when getting the advertising instance. self.simulateExceptionInAdvertiseService(CancelledError()) eventloop.start().wait(2.0) self.addCleanup(lambda: eventloop.reset().wait(5)) response = self.client.get(reverse('rpc-info')) self.assertEqual("application/json", response["Content-Type"]) info = json.loads(response.content.decode("unicode_escape")) self.assertEqual({"eventloops": None}, info)
def stop(self): """ Stop the background pusher. This is safe to call directly from the reactor thread. """ log.msg("%s stopping .." % self.LOGID) self.stopped = True self.failure = CancelledError() ## try cancel any query running .. if self.conn: try: self.conn.cancel() except: pass
def whenConnected(self): """ Retrieve the currently-connected L{Protocol}, or the next one to connect. @return: a Deferred that fires with a protocol produced by the factory passed to C{__init__} @rtype: L{Deferred} firing with L{IProtocol} or failing with L{CancelledError} the service is stopped. """ if self._currentConnection is not None: return succeed(self._currentConnection) elif self._stopped: return fail(CancelledError()) else: result = Deferred() self._awaitingConnected.append(result) return result
def cancel(self): """Cancel all waiters and prevent further use of this object. If a `Deferred` was being captured, it is cancelled, which is a no-op if it has already fired, and this object's reference to it is cleared. If a `Deferred` was being observed, it is *not* cancelled, and this object's reference to it is cleared. After cancelling, `AlreadyCalledError` will be raised if `set` is called, and any `Deferred`s returned from `get` will be already cancelled. """ if self.waiters is None: return self.value = Failure(CancelledError()) waiters, self.waiters = self.waiters, None for waiter in waiters.copy(): waiter.cancel() capturing, self.capturing = self.capturing, None if capturing is not None: capturing.cancel() self.observing = None
def _ignoreAndCancelConnectWaiters(self, f): """ Notify all pending requests for a connection that no more connections are expected, after ignoring the Failure passed in. """ self._unawait(Failure(CancelledError()))
def _cancelConnectWaiters(self): """ Notify all pending requests for a connection that no more connections are expected. """ self._unawait(Failure(CancelledError()))
def close( self, reason: StreamCloseReason, errors: Optional[List[BaseException]] = None, from_protocol: bool = False, ) -> None: """Based on the reason sent we will handle each case. """ if self.metadata['stream_closed_server']: raise StreamClosedError(self.stream_id) if not isinstance(reason, StreamCloseReason): raise TypeError( f'Expected StreamCloseReason, received {reason.__class__.__qualname__}' ) # Have default value of errors as an empty list as # some cases can add a list of exceptions errors = errors or [] if not from_protocol: self._protocol.pop_stream(self.stream_id) self.metadata['stream_closed_server'] = True # We do not check for Content-Length or Transfer-Encoding in response headers # and add `partial` flag as in HTTP/1.1 as 'A request or response that includes # a payload body can include a content-length header field' (RFC 7540 - Section 8.1.2.6) # NOTE: Order of handling the events is important here # As we immediately cancel the request when maxsize is exceeded while # receiving DATA_FRAME's when we have received the headers (not # having Content-Length) if reason is StreamCloseReason.MAXSIZE_EXCEEDED: expected_size = int(self._response['headers'].get( b'Content-Length', self._response['flow_controlled_size'])) error_msg = ( f'Cancelling download of {self._request.url}: received response ' f'size ({expected_size}) larger than download max size ({self._download_maxsize})' ) logger.error(error_msg) self._deferred_response.errback(CancelledError(error_msg)) elif reason is StreamCloseReason.ENDED: self._fire_response_deferred() # Stream was abruptly ended here elif reason is StreamCloseReason.CANCELLED: # Client has cancelled the request. Remove all the data # received and fire the response deferred with no flags set # NOTE: The data is already flushed in Stream.reset_stream() called # immediately when the stream needs to be cancelled # There maybe no :status in headers, we make # HTTP Status Code: 499 - Client Closed Request self._response['headers'][':status'] = '499' self._fire_response_deferred() elif reason is StreamCloseReason.RESET: self._deferred_response.errback( ResponseFailed([ Failure( f'Remote peer {self._protocol.metadata["ip_address"]} sent RST_STREAM', ProtocolError) ])) elif reason is StreamCloseReason.CONNECTION_LOST: self._deferred_response.errback(ResponseFailed(errors)) elif reason is StreamCloseReason.INACTIVE: errors.insert(0, InactiveStreamClosed(self._request)) self._deferred_response.errback(ResponseFailed(errors)) else: assert reason is StreamCloseReason.INVALID_HOSTNAME self._deferred_response.errback( InvalidHostname( self._request, str(self._protocol.metadata['uri'].host, 'utf-8'), f'{self._protocol.metadata["ip_address"]}:{self._protocol.metadata["uri"].port}' ))
def answered(result): """user finally answered our question""" if result: return self.__adduser() else: return Failure(CancelledError())
def _cancelWaitForEmptyPool(self, deferred): self._waitingForEmptyPool.discard(deferred) deferred.errback(CancelledError())
def finishStopping(result): if not self.running: self._stopped = True self._unawait(Failure(CancelledError())) return None
def cancel(self): if self._called: return fail = failure.Failure(CancelledError()) self.errback(fail)