Table of Contents

Class HeartbeatThread

Namespace
OptunaSharp.Storages
Assembly
OptunaSharp.dll

Background thread that records liveness until the owning optimization scope ends.

public sealed class HeartbeatThread : BaseHeartbeatThread, IDisposable
Inheritance
HeartbeatThread
Implements
Inherited Members

Remarks

A failed heartbeat write ends only this thread. Python's _record_heartbeat runs on a threading.Thread; when record_heartbeat raises (a dropped database connection, a busy SQLite file), the thread dies, threading.excepthook prints the error to stderr, and the trial keeps running without heartbeats. .NET instead terminates the whole process on an unhandled thread exception, so this port catches every exception the loop raises, reports it in the excepthook layout (Exception in thread <name>: followed by the exception) on the current standard error, and lets the thread end.

Consequence, as in Optuna: once the heartbeat stops, the trial's heartbeat ages, and any worker's FailStaleTrials(Study) may fail it as stale while its objective is still running. The thread does not retry, because Optuna does not.

Constructors

HeartbeatThread(int, IBaseHeartbeat)

Binds a heartbeat worker to an existing trial; nothing runs until Start().

public HeartbeatThread(int trialId, IBaseHeartbeat heartbeat)

Parameters

trialId int

Storage identifier of the trial; distinct from its per-study trial number.

heartbeat IBaseHeartbeat

Shared heartbeat backend with a non-null interval; this worker does not own the backend.

Methods

Join()

Stops a started worker and blocks until its thread exits.

public override void Join()

Exceptions

NullReferenceException

Start has not initialized the worker.

ThreadInterruptedException

The calling thread is interrupted while joining.

Start()

Starts a background thread that beats immediately and then once per interval until Join(). It is a background thread, so it never keeps the process alive on its own.

public override void Start()

Exceptions

OutOfMemoryException

There is not enough memory to start the thread.