Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup the requests in DMark USSD if we've timed out #1062

Closed
wants to merge 9 commits into from
11 changes: 11 additions & 0 deletions vumi/transports/dmark/dmark_ussd.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ def handle_raw_inbound_message(self, request_id, request):
if session_event == TransportUserMessage.SESSION_NEW:
content = None

# NOTE: Make sure we cleanup any lingering HTTP requests
# in case we timeout. Currently any timed out request
# results in a failure being logged, spamming our Sentry
# instance and mailboxes.
d = request.notifyFinish()
d.addBoth(self.cleanup_request, request_id)

yield self.publish_message(
message_id=request_id,
content=content,
Expand All @@ -174,6 +181,10 @@ def handle_raw_inbound_message(self, request_id, request):
}
})

def cleanup_request(self, error, request_id):
if request_id in self._requests:
self.remove_request(request_id)

@inlineCallbacks
def handle_outbound_message(self, message):
self.emit("DmarkUssdTransport consuming %r" % (message,))
Expand Down
12 changes: 12 additions & 0 deletions vumi/transports/dmark/tests/test_dmark_ussd.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,15 @@ def test_status_down_timeout(self):
self.assertEqual(status['details'], {
'response_time': self.transport.request_timeout + 0.1,
})

@inlineCallbacks
def test_notify_finish_requests_cleanup(self):
self.tx_helper.mk_request()
[msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
[(request_id, request_info)] = self.transport._requests.items()
request = request_info['request']
# Force a request finish, this should call `notifyFinish()`
# which will cleanup the internal _requests registry
request.write('')
request.finish()
self.assertEqual(self.transport._requests, {})