Skip to content

Conversation

@ElFantasma
Copy link
Collaborator

GenServers were cloned before calling handle_cast and handle_call to support a "rollback" functionality if the callback failed on error.

As a result:

  • It was causing a major performance issue on GenServers with a large state and with heavy message traffic.
  • It required all GenServers to implement Clone.
  • This feature showed it was not of use; eventually it is preferred to end the GenServer on errors, instead of rolling back.
  • And actually the feature was not reliable, as the internal state of the GenServer could consist of pointers to external structures (eg. an Arc<Mutex<Store>>) that would suffer modifications during the callback execution and won't rollback on error.

So this removes the cloning of the state on each message, also removes the requirement to implement Clone on every GenServer, and also, it borrows self for message callbacks instead of transferring ownership, making the use a bit more flexible (no need to return a "new_state" on each callback call).

This last change breaks backwards compatibility. Code using this library will need to modify a bit the implemented GenServers

@ElFantasma ElFantasma merged commit a4c6d20 into main Aug 18, 2025
3 checks passed
@MegaRedHand MegaRedHand deleted the no_state_clone branch September 3, 2025 21:31
lakshya-sky added a commit to lakshya-sky/spawned that referenced this pull request Dec 11, 2025
lakshya-sky added a commit to lakshya-sky/spawned that referenced this pull request Dec 11, 2025
ElFantasma pushed a commit that referenced this pull request Dec 12, 2025
* update threads GenServer to not use clone, similar to #42

* add method to get cancellation token

Signed-off-by: lakshya-sky <[email protected]>

---------

Signed-off-by: lakshya-sky <[email protected]>
Co-authored-by: lakshya-sky <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants