From 6f88973f752e73bd5fb96a3ba406dae313d4cb7f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 23 Jun 2021 17:00:30 -0300 Subject: [PATCH] fail longpolling endpoint after 45 seconds. this should fix a possible bug in which connections are left open forever or worse. --- lnbits/core/views/public_api.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lnbits/core/views/public_api.py b/lnbits/core/views/public_api.py index d25d78523..167352acd 100644 --- a/lnbits/core/views/public_api.py +++ b/lnbits/core/views/public_api.py @@ -32,6 +32,24 @@ async def api_public_payment_longpolling(payment_hash): print("adding standalone invoice listener", payment_hash, send_payment) api_invoice_listeners.append(send_payment) - async for payment in receive_payment: - if payment.payment_hash == payment_hash: - return jsonify({"status": "paid"}), HTTPStatus.OK + response = None + + async def payment_info_receiver(cancel_scope): + async for payment in receive_payment: + if payment.payment_hash == payment_hash: + nonlocal response + response = (jsonify({"status": "paid"}), HTTPStatus.OK) + cancel_scope.cancel() + + async def timeouter(cancel_scope): + await trio.sleep(45) + cancel_scope.cancel() + + async with trio.open_nursery() as nursery: + nursery.start_soon(payment_info_receiver, nursery.cancel_scope) + nursery.start_soon(timeouter, nursery.cancel_scope) + + if response: + return response + else: + return jsonify({"message": "timeout"}), HTTPStatus.REQUEST_TIMEOUT