Skip to content

Fix: Disable scheduler warning when scheduler is stopped#926

Merged
benoit-cty merged 4 commits into
masterfrom
copilot/fix-925
Sep 10, 2025
Merged

Fix: Disable scheduler warning when scheduler is stopped#926
benoit-cty merged 4 commits into
masterfrom
copilot/fix-925

Conversation

Copilot AI commented Sep 6, 2025

Copy link
Copy Markdown
Contributor

The background scheduler warning was incorrectly appearing when the scheduler was intentionally stopped, such as during task mode execution.

Problem

When using CodeCarbon's task mode (e.g., tracker.start_task() / tracker.stop_task()) or when the scheduler is manually stopped, users would see inappropriate warnings like:

[codecarbon WARNING] Background scheduler didn't run for a long period (5s), results might be inaccurate

This occurred because:

  1. Task mode intentionally stops the background scheduler to avoid interference with precise task measurements
  2. The _measure_power_and_energy() method is called manually during stop_task()
  3. The warning logic didn't account for when the scheduler was intentionally stopped, triggering false alarms when the time since last measurement exceeded 3× the measurement interval

Solution

Modified the warning condition in _measure_power_and_energy() to check if the scheduler is currently stopped (not self._scheduler._stopped) and skip the warning in that context.

Before:

if last_duration > warning_duration:
    logger.warning(warn_msg, last_duration)

After:

if last_duration > warning_duration and not self._scheduler._stopped:
    logger.warning(warn_msg, last_duration)

Testing

  • ✅ Added comprehensive tests to verify warning behavior when scheduler is stopped vs running
  • ✅ Test confirms warning is suppressed when scheduler is intentionally stopped (e.g., in task mode)
  • ✅ Test confirms warning still appears when scheduler is running but delayed
  • ✅ Verified warning is suppressed in task mode even with long delays
  • ✅ Verified warning still appears correctly in normal mode when appropriate
  • ✅ All existing task tracking tests pass
  • ✅ Task mode examples run without warnings
  • ✅ Multiple task scenarios and edge cases tested

This change preserves the warning's utility in normal mode while eliminating the false positives when the scheduler is intentionally stopped for any reason, not just task mode.

Fixes #925.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add condition to check if we're in task mode (self._active_task is not None)
- Skip scheduler warning when in task mode since scheduler is intentionally stopped
- Preserves warning functionality in normal mode when there are actual delays
- Tested with task mode examples and integration tests

Co-authored-by: benoit-cty <6603048+benoit-cty@users.noreply.github.com>
Copilot AI changed the title [WIP] Disable warning "Background scheduler didn't run for a long period" in task mode Fix: Disable scheduler warning in task mode Sep 6, 2025
Copilot AI requested a review from benoit-cty September 6, 2025 16:03
@benoit-cty

Copy link
Copy Markdown
Contributor

@copilot I think it's better to use if last_duration > warning_duration and not elf._scheduler._stopped

And can you add a test to verify the expected behavior ?

…dd tests

- Replace `self._active_task is None` with `not self._scheduler._stopped` in warning condition
- Add comprehensive tests to verify warning behavior when scheduler is stopped vs running
- Tests confirm warning is suppressed when scheduler is intentionally stopped (e.g., in task mode)
- Tests confirm warning still appears when scheduler should be running but is delayed

Co-authored-by: benoit-cty <6603048+benoit-cty@users.noreply.github.com>

Copilot AI commented Sep 6, 2025

Copy link
Copy Markdown
Contributor Author

@copilot I think it's better to use if last_duration > warning_duration and not elf._scheduler._stopped

And can you add a test to verify the expected behavior ?

Good suggestion! I've updated the implementation to use not self._scheduler._stopped instead of self._active_task is None. This is more direct and robust since it checks the actual state of the scheduler rather than inferring it from task state.

I've also added comprehensive tests as requested:

  1. test_scheduler_warning_suppressed_when_stopped - verifies the warning is suppressed when scheduler is stopped
  2. test_scheduler_warning_shown_when_running - verifies the warning still appears when scheduler is running but delayed

The tests confirm the expected behavior in both scenarios and handle multiple warning types correctly. Changes are in commit eb655b2.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • get.geojs.io
    • Triggering command: python -m pytest tests/test_emissions_tracker.py::TestCarbonTracker::test_scheduler_warning_suppressed_when_stopped -v (dns block)
    • Triggering command: python -m pytest tests/test_emissions_tracker.py::TestCarbonTracker::test_scheduler_warning_suppressed_when_stopped tests/test_emissions_tracker.py::TestCarbonTracker::test_scheduler_warning_shown_when_running -v (dns block)
    • Triggering command: python -m pytest tests/test_emissions_tracker.py -k task -v (dns block)
  • ip-api.com
    • Triggering command: python -m pytest tests/test_emissions_tracker.py::TestCarbonTracker::test_scheduler_warning_suppressed_when_stopped -v (dns block)
    • Triggering command: python -m pytest tests/test_emissions_tracker.py::TestCarbonTracker::test_scheduler_warning_suppressed_when_stopped tests/test_emissions_tracker.py::TestCarbonTracker::test_scheduler_warning_shown_when_running -v (dns block)
    • Triggering command: python -m pytest tests/test_emissions_tracker.py -k task -v (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Fix: Disable scheduler warning in task mode Fix: Disable scheduler warning when scheduler is stopped Sep 6, 2025

@SaboniAmine SaboniAmine 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.

LGTM, thanks Benoît for handling this case!

@benoit-cty benoit-cty marked this pull request as ready for review September 8, 2025 15:31
@benoit-cty benoit-cty merged commit c436078 into master Sep 10, 2025
8 checks passed
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.

Disable warning "Background scheduler didn't run for a long period" in task mode

3 participants