fix(test): create ModelTests before concurrent pool workers - #6040
Conversation
ModelTest.create_test() can call to_datetime()/ttl_cache (time.time()) while another worker freezes time via time_machine for execution_time. Building tests on the main thread removes that race. Fixes SQLMesh#6039 Signed-off-by: devtechedge <devtechedge@users.noreply.github.com>
There was a problem hiding this comment.
Looks pretty close, I didn't notice connections being created. So right after connections are created we should make sure all relevant code is in a try statement to properly cleanup connections. Thanks for picking this up so quickly!
P.S. Don't forget our PR checklist. I added it to your description.
Move calling-thread create_test into the same try/finally that closes engine adapters so invalid tests still clean up connections. Add a NOTE about a possible future parallel create stage that must not overlap runs. Signed-off-by: devtechedge <devtechedge@users.noreply.github.com> Signed-off-by: Dev M <devtechedge@gmail.com>
|
Thanks @cmgoffena13 - addressed your review feedback in 4d515a7:
Also updated the PR checklist for the style/tests/DCO items I ran. |
|
Thanks @cmgoffena13 for the review and for taking the time to walk through the connection cleanup and concurrency details. The feedback around keeping This was a really helpful learning experience for me, both from the concurrency side and from seeing how the implementation should account for cleanup and future extensibility. Glad we could get this merged and close out #6039. Thanks again for the guidance and review! |
Summary
When
sqlmesh testruns withconcurrent_tasks > 1and a unit test setsvars.execution_time,ModelTest.create_test()was running on worker threads. That path can callto_datetime()/ttl_cache(time.time()) while another worker is starting or stopping atime_machinefreeze, which producesIndexError: list index out of range(or similar) during test creation.This moves
ModelTest.create_test()onto the calling thread before submitting work to the pool. Workers only run the already-built tests. Creation stays inside the same try/finally that closes engine adapters so an invalidcreate_teststill cleans up connections.Fixes #6039
Test plan
pytest tests/core/test_test.py::test_freeze_time_concurrent -vpassed 20 consecutive times locally (race; single pass is not enough)Checklist
make styleand fixed any issuesmake fast-test)git commit -s) per the DCO