powermem icon indicating copy to clipboard operation
powermem copied to clipboard

bug: configured E2E startup timeout leaves a live thread using its closed listener

Open hireview001 opened this issue 1 month ago • 1 comments

Describe the bug

tests/e2e/real_experience_skill/harness.py::_start_configured_server closes its listener and raises after its 60-second wait without stopping or joining the still-running startup thread. When legitimate startup finishes later, Uvicorn attempts to use the closed socket and raises OSError: [Errno 9] Bad file descriptor during a subsequent test. The caller never receives a RunningServer, so its normal cleanup cannot stop that thread.

This is a repository real-service test-fixture lifecycle defect. It does not establish a production runtime startup defect or a performance regression. A timeout must clean up the fixture it started, regardless of the chosen startup budget.

Steps to reproduce

At immutable SHA 0c1dfc289b28e9f529555fb57bf771ce51103913, configure Server settings for a disposable embedded SeekDB backend and the real-service inference profile, then run:

python -m pytest tests/e2e/real_experience_skill/test_context_text_assembly.py::test_configured_services_and_native_codex_consume_standard_text tests/e2e/real_experience_skill/test_profile_context_assembly.py::test_configured_profile_generation_review_and_context_delivery --run-real-e2e --real-e2e-env-file=disposable.env -q --junitxml=startup-fixtures.xml

The independently replayed complete failed-node selection produced both startup timeouts and subsequent closed-listener exceptions. To measure actual startup separately with a new random logical database and preserve the listener until shutdown, the following executed probe uses the same configured Server profile with scheduled processing disabled:

import os,json,socket,threading,time,tempfile,uuid
from pathlib import Path
import uvicorn
from powercontext.server.factory import create_server_app
from powercontext.server.settings import ServerSettings
from powercontext.builtin.persistence.seekdb import SeekDBConfig
from tests.e2e.real_experience_skill.harness import _without_scheduled_processing
out=Path(__file__).parent
with tempfile.TemporaryDirectory(prefix="pc-startup-") as tmp:
 os.environ["POWERCONTEXT_HOME"]=str(Path(tmp)/"home")
 settings=_without_scheduled_processing(ServerSettings())
 settings=settings.model_copy(update={"database":SeekDBConfig(path=Path(tmp)/"seekdb",database="pc_probe_"+uuid.uuid4().hex)})
 app=create_server_app(settings=settings,scheduler_path=Path(tmp)/"scheduler.db")
 listener=socket.socket();listener.bind(("127.0.0.1",0));listener.listen(128)
 server=uvicorn.Server(uvicorn.Config(app,log_level="warning"))
 thread=threading.Thread(target=server.run,kwargs={"sockets":[listener]},daemon=True)
 started=time.monotonic();thread.start()
 at60=None
 while thread.is_alive() and not server.started and time.monotonic()-started<180:
  if at60 is None and time.monotonic()-started>=60: at60={"server_started":server.started,"thread_alive":thread.is_alive()}
  time.sleep(.1)
 result={"started":server.started,"elapsed_seconds":round(time.monotonic()-started,3),"at_60_seconds":at60,"fresh_embedded_database":True,"scheduled_processing_disabled":True}
 server.should_exit=True;thread.join(120);listener.close()
 result["thread_stopped"]=not thread.is_alive()
 (out/"startup-timing.json").write_text(json.dumps(result,indent=2));print(json.dumps(result))

Run that probe with the configured Server inference environment and repository test dependencies. Its fresh database/path are always local and temporary. Timing depends on the host; this report makes no universal startup-time claim.

Expected behavior

On startup failure or timeout, request shutdown and wait for the owned server thread before releasing its listener and database resources. Acceptance should not leave background startup running into subsequent tests. The configured backend's actual startup budget should also be considered separately.

Actual behavior

The two original nodes failed after 60.405 and 60.198 seconds with configured PowerContext Server did not start. Later captured stderr contains:

uvicorn/server.py:142 in startup
  server = await loop.create_server(create_protocol, sock=sock, ...)
asyncio/base_events.py:1603 in create_server
  sock.setblocking(False)
OSError: [Errno 9] Bad file descriptor

The independent fresh-database startup probe completed successfully:

{"started": true, "elapsed_seconds": 93.943, "at_60_seconds": {"server_started": false, "thread_alive": true}, "fresh_embedded_database": true, "scheduled_processing_disabled": true, "thread_stopped": true}

This distinguishes a prematurely abandoned fixture from proof that the Server cannot initialize.

Environment

Linux, Python 3.12.13; pylibseekdb 1.4.0.post2; SQLAlchemy 2.0.51; pytest 9.1.1. PowerContext tested SHA 0c1dfc289b28e9f529555fb57bf771ce51103913. Independent JUnit, sanitized stdout/stderr and startup-timing JSON retained. No repository source/tests changed.

Regression-proof: ca399b0f-bf82-41c3-ae54-98dd9f377e98/53d09bffbe4786cb5e0b/configured-startup-cleanup

hireview001 avatar Oct 31 '26 08:10 hireview001

I've opened a fix for this in #2599. Uvicorn finishes lifespan startup before it checks should_exit, so the harness can't interrupt a slow startup. What it can do is never close the listener, or tear down resources the Server uses, while the Server thread is still alive. On a missed deadline it now sets should_exit and waits up to 15 s for the thread. If the thread stops, it releases the resources and fails with "did not start within 60 s; it stopped after shutdown was requested". If the thread is still starting, it fails right away and releases the resources once the thread exits. A late startup then shuts down cleanly on an open socket, so it no longer raises Errno 9 in a later test. That includes the 94 s startup reported here. A startup that crashes is reported separately, with its elapsed time. The baseline _start_server and RestartableServer.start in the ZCode acceptance runner had the same defect and are fixed the same way. The other server.started helpers under tests/e2e/ already stop their thread. Budgets are unchanged. Regression tests use synthetic slow and failing lifespans; the real --run-real-e2e path was not executed.

Fengzdadi avatar Nov 12 '26 04:11 Fengzdadi