fix: create the PollingEventSource timer on start and make it a daemon - #3523
Draft
csviri wants to merge 1 commit into
Draft
fix: create the PollingEventSource timer on start and make it a daemon#3523csviri wants to merge 1 commit into
csviri wants to merge 1 commit into
Conversation
`PollingEventSource` held its timer in a final field initialised at
construction:
private final Timer timer = new Timer();
Two problems follow from that.
The event source cannot be restarted. `stop()` calls `timer.cancel()` and
a cancelled `java.util.Timer` cannot be reused, so a subsequent `start()`
fails with `IllegalStateException: Timer already cancelled`. Restart is a
supported lifecycle - `Operator.stop()` / `start()` recreates the thread
pools for exactly this reason, and `TimerEventSource` creates a new
`Timer` inside `start()`.
The timer thread is not a daemon and is created eagerly. Merely
constructing a `PollingEventSource` therefore starts a non-daemon thread
that keeps the JVM from exiting, even if the event source is never
started, and it outlives an operator that is stopped without stopping its
event sources. `TimerEventSource` uses `new Timer(true)`.
The timer is now created in `start()` as a daemon and cleared in `stop()`,
matching `TimerEventSource`.
Adds regression tests for restart and for the daemon flag; the restart one
fails with `IllegalStateException: Timer already cancelled` without this
change.
Note: `PerResourcePollingEventSource` has a related restart limitation
because `stop()` calls `shutdownNow()` on the `ScheduledExecutorService`
from its configuration. That executor is supplied by the caller, so
changing its ownership semantics is a separate discussion and is left
out of this change.
16 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes PollingEventSource lifecycle and JVM-exit behavior by moving java.util.Timer creation to start() (as a daemon) and clearing it in stop(), aligning its lifecycle with supported operator restart semantics and with TimerEventSource.
Changes:
- Create the
PollingEventSourcetimer onstart()(daemon thread) and null it out onstop(). - Add regression tests covering restart-after-stop and verifying the timer thread is daemon.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/polling/PollingEventSource.java | Timer lifecycle moved to start()/stop() and daemonized to support restart and avoid blocking JVM shutdown. |
| operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/polling/PollingEventSourceTest.java | Adds regression coverage for restart behavior and daemon timer thread behavior. |
Comment on lines
+77
to
+87
| @Test | ||
| void timerThreadIsADaemonSoItDoesNotKeepTheJvmAlive() throws InterruptedException { | ||
| when(resourceFetcher.fetchResources()).thenReturn(testResponseWithTwoValues()); | ||
| pollingEventSource.start(); | ||
| Thread.sleep(DEFAULT_WAIT_PERIOD); | ||
|
|
||
| assertThat(Thread.getAllStackTraces().keySet()) | ||
| .filteredOn(t -> t.getName().startsWith("Timer-")) | ||
| .isNotEmpty() | ||
| .allMatch(Thread::isDaemon); | ||
| } |
Comment on lines
77
to
83
| public void start() throws OperatorException { | ||
| super.start(); | ||
| // a cancelled Timer cannot be reused, so a fresh one is created on every start; it is a daemon | ||
| // thread so that it never keeps the JVM alive | ||
| timer = new Timer(true); | ||
| getStateAndFillCache(); | ||
| timer.schedule( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PollingEventSourceheld its timer in a final field initialised atconstruction:
Two problems follow from that.
The event source cannot be restarted.
stop()callstimer.cancel()anda cancelled
java.util.Timercannot be reused, so a subsequentstart()fails with
IllegalStateException: Timer already cancelled. Restart is asupported lifecycle -
Operator.stop()/start()recreates the threadpools for exactly this reason, and
TimerEventSourcecreates a newTimerinsidestart().The timer thread is not a daemon and is created eagerly. Merely
constructing a
PollingEventSourcetherefore starts a non-daemon threadthat keeps the JVM from exiting, even if the event source is never
started, and it outlives an operator that is stopped without stopping its
event sources.
TimerEventSourceusesnew Timer(true).The timer is now created in
start()as a daemon and cleared instop(),matching
TimerEventSource.Adds regression tests for restart and for the daemon flag; the restart one
fails with
IllegalStateException: Timer already cancelledwithout thischange.
Note:
PerResourcePollingEventSourcehas a related restart limitationbecause
stop()callsshutdownNow()on theScheduledExecutorServicefrom its configuration. That executor is supplied by the caller, so
changing its ownership semantics is a separate discussion and is left
out of this change.
Part of #3517