Skip to content

/reconcile Endpoint Fails on Second and Subsequent Requests #1388

@craigmediaservices

Description

@craigmediaservices

I have encountered an issue with the /reconcile endpoint in the Dashboard. The endpoint works correctly on the first request but fails to load on the second and subsequent requests.

Steps to Reproduce:

  • Start the Flask application.
  • Access the /reconcile endpoint for the first time.
  • Observe that the endpoint responds correctly.
  • Access the /reconcile endpoint again.
  • Observe that the endpoint fails to respond or hangs indefinitely.
from flask import Flask
import asyncio

app = Flask(__name__)

# Global variable to store the cached result
cached_result = None

@app.route("/reconcile")
def reconcile():
    global cached_result  # Use the global variable

    # Check if we have a cached result
    if cached_result is not None:
        return cached_result

    retval = {"gateway_ok": True}
    try:
        asyncio.set_event_loop(asyncio.new_event_loop())
        retval["optimal"] = reporting_api.table_of_optimal_positions().Body
        retval["ib"] = reporting_api.table_of_ib_positions().Body
        retval["my"] = reporting_api.table_of_my_positions().Body

        # Reindex the position dataframes
        retval["ib"].set_index(
            ["instrument_code", "contract_date"], inplace=True, drop=False
        )
        retval["my"].set_index(
            ["instrument_code", "contract_date"], inplace=True, drop=False
        )
    except:
        # IB gateway connection failed
        retval["gateway_ok"] = False
    if "optimal" in retval["optimal"].columns:
        retval["optimal"]["optimal"] = retval["optimal"]["optimal"].astype(str)
    
    retval = dict_of_df_to_dict(retval, orient="index")
    
    # Cache the result
    cached_result = retval
    
    return retval

Specifically it's hanging on retval["ib"] = reporting_api.table_of_ib_positions().Body

I can't replicate the issue manually in Python, but seems to be specifically with the Flask application.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions