scoped sessions should be closed after requests

This commit is contained in:
Matthias 2020-09-11 06:44:20 +02:00
parent 6a08fee25b
commit b8773de5b0
2 changed files with 11 additions and 1 deletions

View File

@ -17,8 +17,9 @@ from werkzeug.serving import make_server
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
from freqtrade.constants import DATETIME_PRINT_FORMAT from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.rpc.rpc import RPC, RPCException from freqtrade.persistence import Trade
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter from freqtrade.rpc.fiat_convert import CryptoToFiatConverter
from freqtrade.rpc.rpc import RPC, RPCException
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -70,6 +71,11 @@ def rpc_catch_errors(func: Callable[..., Any]):
return func_wrapper return func_wrapper
def shutdown_session(exception=None):
# Remove scoped session
Trade.session.remove()
class ApiServer(RPC): class ApiServer(RPC):
""" """
This class runs api server and provides rpc.rpc functionality to it This class runs api server and provides rpc.rpc functionality to it
@ -104,6 +110,8 @@ class ApiServer(RPC):
self.jwt = JWTManager(self.app) self.jwt = JWTManager(self.app)
self.app.json_encoder = ArrowJSONEncoder self.app.json_encoder = ArrowJSONEncoder
self.app.teardown_appcontext(shutdown_session)
# Register application handling # Register application handling
self.register_rest_rpc_urls() self.register_rest_rpc_urls()

View File

@ -471,6 +471,7 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
assert rc.json == {"error": "Error querying _edge: Edge is not enabled."} assert rc.json == {"error": "Error querying _edge: Edge is not enabled."}
@pytest.mark.usefixtures("init_persistence")
def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, limit_sell_order): def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, limit_sell_order):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot, (True, False)) patch_get_signal(ftbot, (True, False))
@ -498,6 +499,7 @@ def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, li
assert rc.json['best_pair'] == '' assert rc.json['best_pair'] == ''
assert rc.json['best_rate'] == 0 assert rc.json['best_rate'] == 0
trade = Trade.query.first()
trade.update(limit_sell_order) trade.update(limit_sell_order)
trade.close_date = datetime.utcnow() trade.close_date = datetime.utcnow()