envoyproxy/envoy

tls: find a generic solution for safely passing objects to worker threads

Open

#9.940 geöffnet am 5. Feb. 2020

Auf GitHub ansehen
 (0 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C++ (5.373 Forks)batch import
help wantedtech debt

Repository-Metriken

Stars
 (27.997 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 8T) (378 gemergte PRs in 30 T)

Beschreibung

When using TLS Slot::set with a callback, it's sometimes useful to add this the to the callback when creating the per thread state. For example, from a filter config constructor, one may do:

FilterConfig::FilterConfig(ThreadLocal::Instance& thread_local):slot_(thread_local.allocateSlot()) {
  slot_->set([this,](
                Event::Dispatcher& dispatcher) -> ThreadLocal::ThreadLocalObjectSharedPtr {
    return this->makeThreadLocalState();
  });
}

This is problematic, as the lifetime of the FilterConfig in the main thread is independent of the lifetime of the callback that runs on the worker threads. And in fact FilterConfig may be destructed by the time the callback is run on the worker threads.

A fix for one such instance, and a discussion on how to solve this was initially done here: https://github.com/envoyproxy/envoy/pull/9884

This issue was created for tracking until a generic solution is achieved. Few notes from the discussion in #9884 :

  • use a weak pointer to move the variables used into the thread callback. not calling the callback if failing to lock the weak pointer.
  • use linting to make sure that this is not passed to Slot::set
  • figure out an easy\generic way to guarantee that the callback uses a valid object. see one such suggestion here

cc @mattklein123

Contributor Guide