Skip to content

Fix/detached forecasting sensors#2035

Open
BelhsanHmida wants to merge 51 commits into
mainfrom
fix/detached-forecasting-sensors
Open

Fix/detached forecasting sensors#2035
BelhsanHmida wants to merge 51 commits into
mainfrom
fix/detached-forecasting-sensors

Conversation

@BelhsanHmida

@BelhsanHmida BelhsanHmida commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Description

Previously, train/predict cycle jobs enqueued a bound TrainPredictPipeline method, which caused the pipeline instance and its ORM-backed sensors/data source to be pickled and sent across the web-worker boundary. In the worker, those objects could be detached from the active DB session and fail with DetachedInstanceError, especially for regressors.

This PR changes cycle jobs to pass only serialized config/parameters plus data_source_id, then reconstructs the pipeline inside the worker. This keeps ORM objects session-local to the worker and avoids carrying session-bound state through RQ.

  • Serialize queued train-predict cycle jobs with plain IDs and timing/config payloads.
  • Reconstruct target sensor, save sensor, regressors, and data source inside the worker DB session.
  • Commit the forecasting DataSource before enqueuing jobs so workers can load it by ID.
  • Keep queued cycle jobs limited to serialized pipeline state and per-cycle timings; arbitrary job_kwargs are intentionally not forwarded.
  • Add regression coverage proving queued job kwargs do not contain ORM instances.
  • Add changelog entry.

Closes #2256

How to test

uv run pytest flexmeasures/data/tests/test_forecasting_pipeline.py flexmeasures/data/tests/test_forecasting_jobs.py flexmeasures/data/tests/test_forecasting_jobs_fresh_db.py flexmeasures/api/v3_0/tests/test_forecasting_api_fresh_db.py flexmeasures/data/tests/test_job_cache.py -q

Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

BelhsanHmida and others added 30 commits March 9, 2026 10:37
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…dated

Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ters

Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ycle job

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
… pipeline

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…ecasts

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…rity

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…n.commit() some time to finish writing a new source to the db

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
This reverts commit 888b980.

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@BelhsanHmida BelhsanHmida self-assigned this Mar 19, 2026
@nhoening nhoening added this to the 0.32.0 milestone Mar 20, 2026
@nhoening nhoening modified the milestones: 0.32.0, 0.33.0 Apr 14, 2026
@nhoening nhoening modified the milestones: 0.33.0, 1.0.0 May 27, 2026
@BelhsanHmida BelhsanHmida marked this pull request as ready for review June 1, 2026 11:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread flexmeasures/data/models/forecasting/pipelines/train_predict.py
BelhsanHmida and others added 2 commits July 6, 2026 09:28
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid detached ORM objects in queued train-predict forecasting jobs

4 participants