I am trying to deploy fasthtml app that uses mongodb search to get some information from the database, the problem is that after i deploy the project and made my first call to the search function it works fine, then anytime i try it again it get the following error:
1Search error: Task <Task pending name='Task-10' coro=<Starlette.__call__() running at /var/task/starlette/applications.py:112> cb=[_run_until_complete_cb() at /var/lang/lib/python3.12/asyncio/base_events.py:182]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at /var/lang/lib/python3.12/asyncio/futures.py:391]> attached to a different loop
code is is
1```python2from fasthtml.common import *3from faq import create_faq4from motor.motor_asyncio import AsyncIOMotorClient5from pymongo import ASCENDING6import os78MONGO_URI = os.environ.get("MONGO_URI")9client = AsyncIOMotorClient(10 MONGO_URI, serverSelectionTimeoutMS=60000, connectTimeoutMS=60000, connect=True11)12db = client.fastcalorie13foods_collection = db.calories14server_info = client.server_info()15print(server_info)1617app, rt = fast_app(18 debug=True,19)202122def mk_search_input():23 return Input(24 id="search-input",25 name="query",26 placeholder="ابحث عن الطعام..",27 required=True,28 )2930@rt("/search")31async def post(query: str):32 try:33 # Create a new cursor for each request34 cursor = (35 foods_collection.find(36 {"$text": {"$search": query}}, {"score": {"$meta": "textScore"}}37 )38 .sort([("score", {"$meta": "textScore"})])39 .limit(20)40 )4142 # Explicitly await the results43 results = []44 async for doc in cursor:45 results.append(doc)4647 if not results:48 return Div("لا توجد نتائج", id="results-table")49 header = Thead(50 Tr(*map(Th, ["الاسم بالعربية", "Food", "السعرات الحرارية"])),51 cls="bg-purple/10",52 )53 rows = [54 Tr(55 Td(56 AX(57 r["arabic_name"],58 f"/food/{r['_id']}",59 "results-table",60 hx_push_url="true",61 )62 ),63 Td(r["name"]),64 Td(r["calories"]),65 cls="even:bg-purple/5",66 )67 for r in results68 ]69 return Table(header, *rows, cls="w-full", id="results-table")70 except Exception as e:71 print(f"Search error: {str(e)}") # Add detailed error logging72 return Div("حدث خطأ في البحث", id="results-table")7374@rt("/food/{id}")75async def get(id: str):76 from bson.objectid import ObjectId7778 food = await foods_collection.find_one({"_id": ObjectId(id)})79 return Table(80 *[Tr(Th(k), Td(v)) for k, v in food.items() if k != "_id"],81 id="results-table",82 )8384@rt("/")85async def get():86 header = Header(87 Nav(88 Ul(89 Li(A("البحث الذكي", href="/")),90 ),91 cls="container",92 ),93 cls="navbar",94 )95 search_form = Form(96 Group(mk_search_input(), Button("Search")),97 hx_post="/search",98 target_id="results-table",99 hx_swap="innerHTML",100 hx_preserve="true",101 hx_indicator="#results-table",102 )103 return (104 Title("كم كالوري"),105 header,106 Main(107 H1("كم كالوري في .."),108 search_form,109 Div(id="results-table"),110 create_faq(),111 cls="container",112 ),113 )114115116serve()117
the project is using fasthtml which is a python package that sues htmx to create web applications similar to fastpi