Skip to content

FIX Each callback context should be closed exactly once - #35447

Merged
lorentzenchr merged 3 commits into
scikit-learn:mainfrom
betatim:fix-repeated-callback-ctx
Nov 19, 2026
Merged

lorentzenchr merged 3 commits into
scikit-learn:mainfrom
betatim:fix-repeated-callback-ctx

Conversation

@betatim

@betatim betatim commented Nov 18, 2026

Copy link
Copy Markdown
Member

Reference Issues/PRs

Follow up to #35334

What does this implement/fix? Explain your changes.

This fixes the problem

import warnings

from sklearn.callback.tests._common.callbacks import StopFitCallback
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

X, y = load_iris(return_X_y=True)
cb = StopFitCallback()
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    LogisticRegression(solver="lbfgs").set_callbacks(cb).fit(X, y)

for r in cb.record:
    print(f"{r['name']:<18} {r['context'].task_name:<11} ctx={id(r['context'])}")

# Every begin should have exactly one matching end.
assert cb.count_hooks("on_fit_task_begin") == cb.count_hooks("on_fit_task_end")

If I run this locally on main I indeed see two ends for the same context:

setup              fit         ctx=5369234832
on_fit_task_begin  fit         ctx=5369234832
on_fit_task_begin  lbfgs-iter  ctx=4426495824
on_fit_task_end    lbfgs-iter  ctx=4426495824
on_fit_task_end    lbfgs-iter  ctx=4426495824
on_fit_task_end    fit         ctx=5369234832
teardown           fit         ctx=5369234832

The first lbfgs-iter end comes from __call__, just before it raises StopIteration. The second comes from close(), which assumes _iter_ctx is always a fresh context that hasn't run yet. That stops being true once the fit is stopped. A minimal fix is to set self._iter_ctx = None before raising, and have close() return early when it's None.

The begin/end count assertion would also be worth adding to test_logistic_regression_fit_stopped_by_callback.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation

cc @lorentzenchr @jeremiedbb @lesteve

@lorentzenchr lorentzenchr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @betatim for the quick follow-up.

@lorentzenchr

Copy link
Copy Markdown
Member

@betatim Could you resolve merge conflicts? Then I‘ll merge.

@lesteve

lesteve commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

(what could possibly go wrong 🙄)

Edit: nothing went wrong because Copilot didn't do anything apparently ...

…ck-ctx

# Conflicts:
#	sklearn/linear_model/_logistic.py
@betatim

betatim commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

I didn't notice there were conflicts :(

Having a (sufficiently powerful) bot that can resolve conflicts would be useful. Then a reviewer can trigger it, take a look and merge or throw it back to the PR author if it looks like it needs human attention to get the resolution right. Reminds me a bit of the "auto merge" button which I at first thought was gimmicky but turns out to be super useful because you can say "merge, assuming all the tests pass". Which means the PR is "done" for you instead of leaving an open loop

@lorentzenchr
lorentzenchr merged commit a8803e4 into scikit-learn:main Nov 19, 2026
38 checks passed
@betatim
betatim deleted the fix-repeated-callback-ctx branch November 19, 2026 16:13
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.

3 participants