-
Notifications
You must be signed in to change notification settings - Fork 207
fix(actor-typed): discard stale ReceiveTimeout after cancelReceiveTimeout to avoid NPE #3086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
actor-typed-tests/src/test/scala/org/apache/pekko/actor/typed/CancelReceiveTimeoutSpec.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.pekko.actor.typed | ||
|
|
||
| import scala.concurrent.duration._ | ||
| import scala.reflect.ClassTag | ||
|
|
||
| import org.apache.pekko | ||
| import pekko.actor.testkit.typed.scaladsl.LogCapturing | ||
| import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit | ||
| import pekko.actor.testkit.typed.scaladsl.TestProbe | ||
| import pekko.actor.typed.scaladsl.Behaviors | ||
| import pekko.actor.typed.scaladsl.adapter._ | ||
|
|
||
| import org.scalatest.wordspec.AnyWordSpecLike | ||
|
|
||
| class CancelReceiveTimeoutSpec extends ScalaTestWithActorTestKit with AnyWordSpecLike with LogCapturing { | ||
|
|
||
| sealed trait Command | ||
| case object SetAndCancelTimeout extends Command | ||
| case object Timeout extends Command | ||
| case object Ping extends Command | ||
| case object Pong extends Event | ||
|
|
||
| sealed trait Event | ||
| case object Done extends Event | ||
|
|
||
| // A no-op interceptor that deepens the behavior stack so the message | ||
| // passes through InterceptorImpl.receive — the exact code path where | ||
| // msg.getClass throws NPE on a null message. | ||
| private def noopInterceptor[T: ClassTag] = new BehaviorInterceptor[T, T] { | ||
| override def aroundReceive( | ||
| ctx: TypedActorContext[T], | ||
| msg: T, | ||
| target: BehaviorInterceptor.ReceiveTarget[T]): Behavior[T] = | ||
| target(ctx, msg) | ||
| } | ||
|
|
||
| "A typed actor with receive timeout" must { | ||
|
|
||
| // Regression test for #3084: cancelReceiveTimeout() sets receiveTimeoutMsg to | ||
| // null, but a classic ReceiveTimeout already enqueued in the mailbox cannot be | ||
| // retracted. Before the fix the stale ReceiveTimeout was forwarded to the typed | ||
| // behavior stack as a null message, causing NPE in InterceptorImpl.receive. | ||
| "silently discard a stale ReceiveTimeout after cancelReceiveTimeout" in { | ||
| val probe = TestProbe[Event]() | ||
|
|
||
| def behavior: Behavior[Command] = | ||
| Behaviors.setup { context => | ||
| // Wrap in an interceptor so the message traverses InterceptorImpl.receive, | ||
| // the exact location where msg.getClass throws NPE on a null message. | ||
| Behaviors.intercept[Command, Command](() => noopInterceptor[Command]) { | ||
| Behaviors.receiveMessage { | ||
| case SetAndCancelTimeout => | ||
| // Set a very long timeout (won't actually fire), then immediately | ||
| // cancel it. This leaves receiveTimeoutMsg as null. | ||
| context.setReceiveTimeout(1.hour, Timeout) | ||
| context.cancelReceiveTimeout() | ||
| probe.ref ! Done | ||
| Behaviors.same | ||
|
|
||
| case Timeout => | ||
| // Should never reach here: the timeout was cancelled and we send | ||
| // the classic ReceiveTimeout ourselves to simulate the stale case. | ||
| Behaviors.unhandled | ||
|
|
||
| case Ping => | ||
| probe.ref ! Pong | ||
| Behaviors.same | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val ref = spawn(behavior) | ||
|
|
||
| // Step 1: actor sets and cancels the receive timeout → receiveTimeoutMsg is now null | ||
| ref ! SetAndCancelTimeout | ||
| probe.expectMessage(Done) | ||
|
|
||
| // Step 2: simulate a stale classic ReceiveTimeout arriving in the mailbox. | ||
| // This is what happens when the scheduler fires ReceiveTimeout before | ||
| // cancelReceiveTimeout() is processed but the actor dequeues them in the | ||
| // opposite order. | ||
| ref.toClassic ! pekko.actor.ReceiveTimeout | ||
|
|
||
| // Step 3: verify the actor is still alive and responsive (no NPE crash). | ||
| ref ! Ping | ||
| probe.expectMessage(Pong) | ||
| } | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.