Skip to content

Commit 0f6c477

Browse files
committed
scala/scala3#8662 - todo rollback
1 parent 529caa7 commit 0f6c477

File tree

11 files changed

+124
-77
lines changed

11 files changed

+124
-77
lines changed

akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/BehaviorTestKitImpl.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,20 @@ private[akka] final class BehaviorTestKitImpl[T](_path: ActorPath, _initialBehav
122122
}
123123

124124
override def run(message: T): Unit = {
125-
try {
125+
akka.try8662 {
126126
currentUncanonical = Behavior.interpretMessage(current, context, message)
127127
current = Behavior.canonicalize(currentUncanonical, current, context)
128128
runAllTasks()
129-
} catch handleException
129+
} _catch handleException
130130
}
131131

132132
override def runOne(): Unit = run(selfInbox().receiveMessage())
133133

134134
override def signal(signal: Signal): Unit = {
135-
try {
135+
akka.try8662 {
136136
currentUncanonical = Behavior.interpretSignal(current, context, signal)
137137
current = Behavior.canonicalize(currentUncanonical, current, context)
138-
} catch handleException
138+
} _catch handleException
139139
}
140140

141141
override def hasEffects(): Boolean = !context.effectQueue.isEmpty

akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestKitUtils.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ private[akka] object ActorTestKitGuardian {
3434

3535
val testKitGuardian: Behavior[TestKitCommand] = Behaviors.receive[TestKitCommand] {
3636
case (context, SpawnActor(name, behavior, reply, props)) =>
37-
try {
37+
akka.try8662[Behavior[TestKitCommand]] {
3838
reply ! context.spawn(behavior, name, props)
3939
Behaviors.same
40-
} catch handleSpawnException(context, reply, props)
40+
} _catch handleSpawnException(context, reply, props)
4141
case (context, SpawnActorAnonymous(behavior, reply, props)) =>
42-
try {
42+
akka.try8662[Behavior[TestKitCommand]] {
4343
reply ! context.spawnAnonymous(behavior, props)
4444
Behaviors.same
45-
} catch handleSpawnException(context, reply, props)
45+
} _catch handleSpawnException(context, reply, props)
4646
case (context, StopActor(ref, reply)) =>
4747
context.watchWith(ref, ActorStopped(reply))
4848
context.stop(ref)

akka-actor-typed/src/main/scala/akka/actor/typed/internal/Supervision.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ private abstract class AbstractSupervisor[I, Thr <: Throwable](strategy: Supervi
7272
}
7373

7474
override def aroundStart(ctx: TypedActorContext[Any], target: PreStartTarget[I]): Behavior[I] = {
75-
try {
75+
akka.try8662 {
7676
target.start(ctx)
77-
} catch handleExceptionOnStart(ctx, target)
77+
} _catch handleExceptionOnStart(ctx, target)
7878
}
7979

8080
override def aroundSignal(ctx: TypedActorContext[Any], signal: Signal, target: SignalTarget[I]): Behavior[I] = {
81-
try {
81+
akka.try8662 {
8282
target(ctx, signal)
83-
} catch handleSignalException(ctx, target)
83+
} _catch handleSignalException(ctx, target)
8484
}
8585

8686
def log(ctx: TypedActorContext[_], t: Throwable): Unit = {
@@ -119,9 +119,9 @@ private abstract class SimpleSupervisor[T, Thr <: Throwable: ClassTag](ss: Super
119119
extends AbstractSupervisor[T, Thr](ss) {
120120

121121
override def aroundReceive(ctx: TypedActorContext[Any], msg: Any, target: ReceiveTarget[T]): Behavior[T] = {
122-
try {
122+
akka.try8662 {
123123
target(ctx, msg.asInstanceOf[T])
124-
} catch handleReceiveException(ctx, target)
124+
} _catch handleReceiveException(ctx, target)
125125
}
126126

127127
protected def handleException(@unused ctx: TypedActorContext[Any]): Catcher[Behavior[T]] = {
@@ -258,9 +258,9 @@ private class RestartSupervisor[T, Thr <: Throwable: ClassTag](initial: Behavior
258258
case m: T @unchecked =>
259259
restartingInProgress match {
260260
case OptionVal.None =>
261-
try {
261+
akka.try8662 {
262262
target(ctx, m)
263-
} catch handleReceiveException(ctx, target)
263+
} _catch handleReceiveException(ctx, target)
264264
case OptionVal.Some((stashBuffer, _)) =>
265265
if (stashBuffer.isFull)
266266
dropped(ctx, m)
@@ -369,7 +369,7 @@ private class RestartSupervisor[T, Thr <: Throwable: ClassTag](initial: Behavior
369369
case _: Restart =>
370370
}
371371

372-
try {
372+
akka.try8662[Behavior[T]] {
373373
val newBehavior = Behavior.validateAsInitial(Behavior.start(initial, ctx.asInstanceOf[TypedActorContext[T]]))
374374
val nextBehavior = restartingInProgress match {
375375
case OptionVal.None => newBehavior
@@ -378,7 +378,7 @@ private class RestartSupervisor[T, Thr <: Throwable: ClassTag](initial: Behavior
378378
stashBuffer.unstashAll(newBehavior.unsafeCast)
379379
}
380380
nextBehavior.narrow
381-
} catch handleException(ctx, signalRestart = {
381+
} _catch handleException(ctx, signalRestart = {
382382
case e: UnstashException[Any] @unchecked => Behavior.interpretSignal(e.behavior, ctx, PreRestart)
383383
case _ => ()
384384
})

akka-actor-typed/src/main/scala/akka/actor/typed/internal/adapter/ActorAdapter.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ import scala.util.control.NonFatal
108108
}
109109

110110
private def handleMessage(msg: T): Unit = {
111-
try {
111+
akka.try8662 {
112112
val c = ctx
113113
if (c.hasTimer) {
114114
msg match {
@@ -124,13 +124,13 @@ import scala.util.control.NonFatal
124124
} else {
125125
next(Behavior.interpretMessage(behavior, c, msg), msg)
126126
}
127-
} catch handleUnstashException
127+
} _catch handleUnstashException
128128
}
129129

130130
private def handleSignal(sig: Signal): Unit = {
131-
try {
131+
akka.try8662 {
132132
next(Behavior.interpretSignal(behavior, ctx, sig), sig)
133-
} catch handleUnstashException
133+
} _catch handleUnstashException
134134
}
135135

136136
private def handleUnstashException: Catcher[Unit] = {

akka-actor/src/main/scala/akka/actor/ActorCell.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ private[akka] class ActorCell(
501501
val rest = messages.tail
502502
val message = messages.head
503503
message.unlink()
504-
try {
504+
akka.try8662 {
505505
message match {
506506
case message: SystemMessage if shouldStash(message, currentState) => stash(message)
507507
case f: Failed => handleFailure(f)
@@ -516,7 +516,7 @@ private[akka] class ActorCell(
516516
case Supervise(child, async) => supervise(child, async)
517517
case NoMessage => // only here to suppress warning
518518
}
519-
} catch handleNonFatalOrInterruptedException { e =>
519+
} _catch handleNonFatalOrInterruptedException { e =>
520520
handleInvokeFailure(Nil, e)
521521
}
522522
val newState = calculateState
@@ -535,17 +535,17 @@ private[akka] class ActorCell(
535535
final def invoke(messageHandle: Envelope): Unit = {
536536
val msg = messageHandle.message
537537
val timeoutBeforeReceive = cancelReceiveTimeoutIfNeeded(msg)
538-
try {
538+
akka.try8662 {
539539
currentMessage = messageHandle
540540
if (msg.isInstanceOf[AutoReceivedMessage]) {
541541
autoReceiveMessage(messageHandle)
542542
} else {
543543
receiveMessage(msg)
544544
}
545545
currentMessage = null // reset current message after successful invocation
546-
} catch handleNonFatalOrInterruptedException { e =>
546+
} _catchF handleNonFatalOrInterruptedException { e =>
547547
handleInvokeFailure(Nil, e)
548-
} finally
548+
} _finally
549549
// Schedule or reschedule receive timeout
550550
checkReceiveTimeoutIfNeeded(msg, timeoutBeforeReceive)
551551
}

akka-actor/src/main/scala/akka/actor/dungeon/Dispatch.scala

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,32 +133,36 @@ private[akka] trait Dispatch { this: ActorCell =>
133133

134134
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
135135
final def suspend(): Unit =
136-
try dispatcher.systemDispatch(this, Suspend())
137-
catch handleException
136+
akka.try8662 {
137+
dispatcher.systemDispatch(this, Suspend())
138+
} _catch handleException
138139

139140
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
140141
final def resume(causedByFailure: Throwable): Unit =
141-
try dispatcher.systemDispatch(this, Resume(causedByFailure))
142-
catch handleException
142+
akka.try8662 {
143+
dispatcher.systemDispatch(this, Resume(causedByFailure))
144+
} _catch handleException
143145

144146
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
145147
final def restart(cause: Throwable): Unit =
146-
try dispatcher.systemDispatch(this, Recreate(cause))
147-
catch handleException
148+
akka.try8662 {
149+
dispatcher.systemDispatch(this, Recreate(cause))
150+
} _catch handleException
148151

149152
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
150153
final def stop(): Unit =
151-
try dispatcher.systemDispatch(this, Terminate())
152-
catch handleException
154+
akka.try8662 {
155+
dispatcher.systemDispatch(this, Terminate())
156+
} _catch handleException
153157

154158
def sendMessage(msg: Envelope): Unit =
155-
try {
159+
akka.try8662 {
156160
val msgToDispatch =
157161
if (system.settings.SerializeAllMessages) serializeAndDeserialize(msg)
158162
else msg
159163

160164
dispatcher.dispatch(this, msgToDispatch)
161-
} catch handleException
165+
} _catch handleException
162166

163167
private def serializeAndDeserialize(envelope: Envelope): Envelope = {
164168

@@ -205,7 +209,8 @@ private[akka] trait Dispatch { this: ActorCell =>
205209
}
206210

207211
override def sendSystemMessage(message: SystemMessage): Unit =
208-
try dispatcher.systemDispatch(this, message)
209-
catch handleException
212+
akka.try8662 {
213+
dispatcher.systemDispatch(this, message)
214+
} _catch handleException
210215

211216
}

akka-actor/src/main/scala/akka/actor/dungeon/FaultHandling.scala

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ private[akka] trait FaultHandling { this: ActorCell =>
6363
if (system.settings.DebugLifecycle) publish(Debug(self.path.toString, clazz(failedActor), "restarting"))
6464
if (failedActor ne null) {
6565
val optionalMessage = if (currentMessage ne null) Some(currentMessage.message) else None
66-
try {
66+
akka.try8662 {
6767
// if the actor fails in preRestart, we can do nothing but log it: it’s best-effort
6868
if (failedActor.context ne null) failedActor.aroundPreRestart(cause, optionalMessage)
69-
} catch handleNonFatalOrInterruptedException { e =>
69+
} _catchF handleNonFatalOrInterruptedException { e =>
7070
val ex = PreRestartException(self, e, cause, optionalMessage)
7171
publish(Error(ex, self.path.toString, clazz(failedActor), e.getMessage))
72-
} finally {
72+
} _finally {
7373
clearActorFields(failedActor, recreate = true)
7474
}
7575
}
@@ -134,8 +134,9 @@ private[akka] trait FaultHandling { this: ActorCell =>
134134
private def finishCreate(): Unit = {
135135
try resumeNonRecursive()
136136
finally clearFailed()
137-
try create(None)
138-
catch handleNonFatalOrInterruptedException { e =>
137+
akka.try8662 {
138+
create(None)
139+
} _catch handleNonFatalOrInterruptedException { e =>
139140
handleInvokeFailure(Nil, e)
140141
}
141142
}
@@ -176,7 +177,7 @@ private[akka] trait FaultHandling { this: ActorCell =>
176177

177178
final def handleInvokeFailure(childrenNotToSuspend: immutable.Iterable[ActorRef], t: Throwable): Unit = {
178179
// prevent any further messages to be processed until the actor has been restarted
179-
if (!isFailed) try {
180+
if (!isFailed) akka.try8662 {
180181
suspendNonRecursive()
181182
// suspend children
182183
val skip: Set[ActorRef] = currentMessage match {
@@ -193,7 +194,7 @@ private[akka] trait FaultHandling { this: ActorCell =>
193194
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
194195
parent.sendSystemMessage(Failed(self, t, uid))
195196
}
196-
} catch handleNonFatalOrInterruptedException { e =>
197+
} _catch handleNonFatalOrInterruptedException { e =>
197198
publish(
198199
Error(
199200
e,
@@ -212,30 +213,33 @@ private[akka] trait FaultHandling { this: ActorCell =>
212213
* Please note that if a parent is also a watcher then ChildTerminated and Terminated must be processed in this
213214
* specific order.
214215
*/
215-
try if (a ne null) a.aroundPostStop()
216-
catch handleNonFatalOrInterruptedException { e =>
216+
akka.try8662 {
217+
if (a ne null) a.aroundPostStop()
218+
} _catchF handleNonFatalOrInterruptedException { e =>
217219
publish(Error(e, self.path.toString, clazz(a), e.getMessage))
218-
} finally try dispatcher.detach(this)
219-
finally try parent.sendSystemMessage(
220-
DeathWatchNotification(self, existenceConfirmed = true, addressTerminated = false))
221-
finally try stopFunctionRefs()
222-
finally try tellWatchersWeDied()
223-
finally try unwatchWatchedActors(a) // stay here as we expect an emergency stop from handleInvokeFailure
224-
finally {
225-
if (system.settings.DebugLifecycle)
226-
publish(Debug(self.path.toString, clazz(a), "stopped"))
227-
228-
clearActorFields(a, recreate = false)
229-
clearActorCellFields(this)
230-
actor = null
220+
} _finally {
221+
try dispatcher.detach(this)
222+
finally try parent.sendSystemMessage(
223+
DeathWatchNotification(self, existenceConfirmed = true, addressTerminated = false))
224+
finally try stopFunctionRefs()
225+
finally try tellWatchersWeDied()
226+
finally try unwatchWatchedActors(a) // stay here as we expect an emergency stop from handleInvokeFailure
227+
finally {
228+
if (system.settings.DebugLifecycle)
229+
publish(Debug(self.path.toString, clazz(a), "stopped"))
230+
231+
clearActorFields(a, recreate = false)
232+
clearActorCellFields(this)
233+
actor = null
234+
}
231235
}
232236
}
233237

234238
private def finishRecreate(cause: Throwable, failedActor: Actor): Unit = {
235239
// need to keep a snapshot of the surviving children before the new actor instance creates new ones
236240
val survivors = children
237241

238-
try {
242+
akka.try8662 {
239243
try resumeNonRecursive()
240244
finally clearFailed() // must happen in any case, so that failure is propagated
241245

@@ -250,11 +254,12 @@ private[akka] trait FaultHandling { this: ActorCell =>
250254
// only after parent is up and running again do restart the children which were not stopped
251255
survivors.foreach(
252256
child =>
253-
try child.asInstanceOf[InternalActorRef].restart(cause)
254-
catch handleNonFatalOrInterruptedException { e =>
257+
akka.try8662 {
258+
child.asInstanceOf[InternalActorRef].restart(cause)
259+
} _catch handleNonFatalOrInterruptedException { e =>
255260
publish(Error(e, self.path.toString, clazz(freshActor), "restarting " + child))
256261
})
257-
} catch handleNonFatalOrInterruptedException { e =>
262+
} _catch handleNonFatalOrInterruptedException { e =>
258263
clearActorFields(actor, recreate = false) // in order to prevent preRestart() from happening again
259264
handleInvokeFailure(survivors, PostRestartException(self, e, cause))
260265
}
@@ -290,8 +295,9 @@ private[akka] trait FaultHandling { this: ActorCell =>
290295
* below will hit the empty default case, too)
291296
*/
292297
if (actor != null) {
293-
try actor.supervisorStrategy.handleChildTerminated(this, child, children)
294-
catch handleNonFatalOrInterruptedException { e =>
298+
akka.try8662 {
299+
actor.supervisorStrategy.handleChildTerminated(this, child, children)
300+
} _catch handleNonFatalOrInterruptedException { e =>
295301
publish(Error(e, self.path.toString, clazz(actor), "handleChildTerminated failed"))
296302
handleInvokeFailure(Nil, e)
297303
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
3+
*/
4+
5+
package akka
6+
7+
/*
8+
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
9+
*/
10+
11+
// todo remove after this issue is fixed:
12+
// https://github.com/lampepfl/dotty/issues/8662
13+
// note: there are many other place affected by this issue but we only hacking here
14+
// so akka-actor-typed-tests/test passed
15+
private[akka] object try8662 {
16+
class Catch[T](f: => T) {
17+
def _catch(catchHandler: => PartialFunction[Throwable, T]): T =
18+
try f catch { case e: Throwable =>
19+
val h = catchHandler
20+
if (h.isDefinedAt(e)) h(e)
21+
else throw e
22+
}
23+
def _catchF(ch: => PartialFunction[Throwable, T]): Finally[T] = new Finally(f, ch)
24+
}
25+
class Finally[T](f: => T, catchHandler: => PartialFunction[Throwable, T]) {
26+
def _finally(finallyHandler: => Unit): T =
27+
try f catch { case e: Throwable =>
28+
val h = catchHandler
29+
if (h.isDefinedAt(e)) h(e)
30+
else throw e
31+
} finally finallyHandler
32+
}
33+
def apply[T](f: => T): Catch[T] = new Catch(f)
34+
}

0 commit comments

Comments
 (0)