softwaremill / softwaremill/ox

Actors impl can easily lead to multithreading issues

Open
#366 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
522
Forks
35
Avg merge
45m
Merged PRs (30d)
13

Description

I think the way ask & tell are impl they can lead to unwanted behaviour, i.e. say we have an actor that can calculate the max of a list. We would expect the actor to be called with a readily available list, but a call like:

actorRef.tell(_.max(calcMyList(...)))

means that the actor thread will also have to calculate the list.

The correct way to call it would be:

val l=calcMyList(...)
actorRef.tell(_.max(l))

but this can easily be missed by the developer writing the code, leading to slow throughput of the actor (because it is also calculating arbitrary lists, not just their maximums)

More silly code could lead to unpredictable behaviour:

var i=0
actor.tell(_.addOne(i))
while true do i+=1

// what will be the arg value to addOne?

Is there a solution to this? Maybe if:

actorRef.tell(_.max , calcMyList(...)) 

where the tell and ask methods take a FunctionN and then separately by-value their arguments like

class ActorRef...:
   def tell[A](f:Function1[A,Unit],arg1:A) ...

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

Start by reviewing the ask and tell implementations referenced in the issue, then trace how closures and their captured arguments are executed. Compare the proposed separate-argument API with the current behavior and determine the intended safety guarantees. Done means an agreed API design that prevents caller-side computation and mutable-value races, with corresponding coverage identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.