ruby / ruby/weakref

WeakRef requires rescuing RefError to avoid race condition

Open
#3 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
20
Forks
8
Avg merge
9h 32m
Merged PRs (30d)
1

Description

There's an race condition in the implied usage of WeakRef.

The API only has weakref_alive?, and then delegated access to the referenced object. But the delegated access to the object cannot be protected by weakref_alive? since GC may occur between the check and the usage.

This means we basically always have to check for RefError, which basically makes weakref_alive? useless if we want to actually potentially use the object.

WeakMap usage is discouraged, leaving us with needing to add this functionality to WeakRef, which may break if WeakRef implementation changes (i.e., there is no good solution for this).

This is discussed at length here:

https://stackoverflow.com/questions/69185508/ruby-weakref-has-implicit-race-condition

I would recommend an addition to the API that will safely return a (non-weak) object if it's alive, or else nil, and obviously it's up to the user to realize that this will stop GC from happening on that object while they hold it.

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 with the WeakRef API, especially weakref_alive?, delegated access, RefError handling, and the discouraged WeakMap alternative. Read the linked Stack Overflow discussion for the race condition context; done means agreeing on and implementing an API that safely returns a strong referenced object when alive or nil otherwise.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.