ruby / ruby/weakref

WeakRef requires rescuing RefError to avoid race condition

Offen
#3 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Ruby
Sterne
20
Forks
8
Ø Merge
9 Std. 32 Min.
Gemergte PRs (30 T.)
1

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der WeakRef API, insbesondere mit weakref_alive?, dem delegierten Zugriff, der Behandlung von RefError und der nicht empfohlenen WeakMap-Alternative. Lesen Sie die verlinkte Stack Overflow-Diskussion zum Kontext der Race Condition; abgeschlossen ist die Aufgabe, wenn Sie sich auf eine API geeinigt und diese implementiert haben, die sicher ein stark referenziertes Objekt zurückgibt, wenn es noch lebt, oder andernfalls nil.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
ruby
Bereich
backend
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.