ruby / ruby/weakref

WeakRef requires rescuing RefError to avoid race condition

Aperta
#3 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Ruby
Stelle
20
Fork
8
Merge medio
9h 32m
PR unite (30g)
1

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’API WeakRef, in particolare con weakref_alive?, l’accesso delegato, la gestione di RefError e l’alternativa sconsigliata WeakMap. Leggi la discussione collegata su Stack Overflow per il contesto della race condition; il lavoro è concluso quando si è concordata e implementata un’API che restituisca in sicurezza un oggetto con un riferimento forte quando è ancora vivo, oppure nil in caso contrario.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
ruby
Ambito
backend
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.