Skip to content

Handle WeakActorRef::upgrade() failure gracefully in EngineActor #129

@coderabbitai

Description

@coderabbitai

Problem

In crates/libtortillas/src/engine/actor.rs around lines 133-134, there's a potential panic when calling actor_ref.upgrade().unwrap() in the TCP and uTP peer stream handling logic.

Current Code

Some(Signal::Message {
   message: Box::new(EngineMessage::IncomingPeer(peer_stream)),
   actor_ref: actor_ref.upgrade().unwrap(),
   reply: None,
   sent_within_actor: true,
})

Issue

If the weak actor reference upgrade fails (returns None), the application will panic. This can happen in legitimate scenarios where the actor has been dropped.

Suggested Fix

Handle the upgrade failure gracefully:

let Some(actor_ref) = actor_ref.upgrade() else {
   error!("Failed to upgrade weak actor reference");
   return None;
};
Some(Signal::Message {
   message: Box::new(EngineMessage::IncomingPeer(peer_stream)),
   actor_ref,
   reply: None,
   sent_within_actor: true,
})

Context

This issue affects both TCP and uTP peer stream handling in the EngineActor::next method.

Backlinks

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglow prioLow priority issue

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions