Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Shard: a failed lease release in postStop is silently ignored

Open Beginner friendly
#3,705 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
scala

Research direction

Start in cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/Shard.scala around lines 1236-1240 where postStop releases the lease. Read the surrounding Shard actor to see how logging is done, then apply the suggested onComplete callback on the release Future. Add a test in ShardWithLeaseSpec or ClusterShardingLeaseSpec using TestLease.setNextReleaseResult to assert a failed release logs an error. Done when the new warning/error logs fire and existing tests pass.

Written by the indexing model from the issue text.

Description

Summary

Shard releases its lease in postStop and discards the returned Future, so a failed release is never logged. This is the same class of problem as #3685 (fixed for ClusterSingletonManager in #3699), but in cluster sharding.

Evidence

cluster-sharding/src/main/scala/org/apache/pekko/cluster/sharding/Shard.scala:1236-1240:

override def postStop(): Unit = {
  passivateIntervalTask.foreach(_.cancel())
  lease.foreach(_.release())
  log.debug("{}: Shard [{}] shutting down", typeName, shardId)
}

Lease.release() returns Future[Boolean]. Here both a failed future and a false result (lease not released) are dropped silently.

Impact

Observability rather than correctness. When the lease backend is unavailable as a shard stops (hand-off, rebalance, region shutdown), the lease is not released and nothing says so. The shard's next allocation then waits until the lease times out, and the logs give no reason for the stall.

Suggested direction

The shard is stopping, so the result can't be piped back to self. Instead, log directly from the future's callback; the ActorLogging adapter is safe to use from another thread. For example:

lease.foreach { l =>
  import context.dispatcher
  val tn = typeName
  val id = shardId
  l.release().onComplete {
    case Success(true)  =>
    case Success(false) => log.warning("{}: Lease for shard [{}] was not released", tn, id)
    case Failure(t)     => log.error(t, "{}: Failed to release lease for shard [{}]", tn, id)
  }
}

Also consider guarding against a release() that throws synchronously, so the rest of postStop still runs.

Test

ShardWithLeaseSpec / ClusterShardingLeaseSpec use TestLease. #3699 adds TestLease.setNextReleaseResult, which allows a case asserting that a failed release logs an error when a shard stops.

Dominant language
Scala
Stars
1.7k
Forks
212
Avg merge
21h 9m
Merged PRs (30d)
98

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/pekko

All issues in apache/pekko

Similar issues

More Scala issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.