Skip to content

Conversation

@Quant-Quasar
Copy link

Motivation

When using OptunaSearchCV with n_jobs > 1, users may pass stateful callback objects
(e.g., LightGBM early stopping callbacks) via fit_params.

Because fit_params is shared across parallel workers, callback objects inside it can
be shared by reference, leading to unintended cross-trial interference.
In practice, this can cause multiple parallel trials to observe and mutate the same
callback state (such as best_iteration), resulting in incorrect or identical early
stopping behavior across trials.

This issue was reported in #240 and can silently affect optimization results when running
parallel cross-validation with callbacks.

Description of the changes

This PR ensures proper isolation of callback state across parallel trials by:

  • Creating a shallow copy of fit_params at objective execution time.
  • Deep-copying only the callbacks entry (if present) to provide each trial with an
    independent callback instance.
  • Leaving all other entries in fit_params unchanged to avoid unnecessary duplication
    of large or expensive objects.

The change is localized to the objective execution path and does not affect existing
behavior for single-threaded runs or workflows that do not use callbacks.

This prevents shared mutable state between parallel trials while keeping memory and
performance overhead minimal.

@Quant-Quasar Quant-Quasar force-pushed the fix-issue-240-shared-callbacks branch 2 times, most recently from 76e5c15 to 05662a1 Compare December 26, 2025 08:06
@Quant-Quasar Quant-Quasar force-pushed the fix-issue-240-shared-callbacks branch from 05662a1 to 6a15eac Compare December 26, 2025 08:18
@not522 not522 self-assigned this Dec 26, 2025
@not522
Copy link
Member

not522 commented Dec 26, 2025

Thank you for your PR! Since we're approaching the year-end and New Year holidays, I'll review the details after New Year's Day.

@Quant-Quasar
Copy link
Author

Thank you for the update! I completely understand.
Please take your time, and I’m happy to follow up after New Year’s.

@github-actions
Copy link

github-actions bot commented Jan 4, 2026

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Jan 4, 2026
@c-bata c-bata removed the stale Exempt from stale bot labeling. label Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants