Skip to content

Android: per-runtime statics are read and written from runtime threads without a lock #99

Open

Description

@NathanWalker

Summary

Several process-wide statics hold one entry per runtime, or one value set by every runtime. Each runtime reads and writes them from its own thread, with no lock. When workers start or stop while other runtimes are running, these are data races of the same kind as #93: a hash table can rehash under a concurrent reader, and a std::string can be reallocated under a concurrent reader.

Static Where Written Read
JSR::env_to_jsr_cache (std::unordered_map) NativeScript/napi/v8/jsr.cpp js_create_napi_env, js_free_napi_env js_lock_env / js_unlock_env
MetadataNode::s_arrayObjects (robin_hood::unordered_map) NativeScript/ffi/jni/{napi,jsi}/metadata/MetadataNode.cpp first array-constructor lookup in each runtime; erased on dispose every array-constructor lookup
envToPersistentSmartJSONStringify / rtToPersistentSmartJSONStringify (robin_hood::unordered_map) NativeScript/ffi/jni/{napi,jsi}/global/GlobalHelpers.cpp first JsonStringifyObject in each runtime; erased on dispose every JsonStringifyObject
Constants::APP_ROOT_FOLDER_PATH (std::string) NativeScript/ffi/jni/{napi,jsi}/constants/Constants.cpp assigned in Runtime::Init of every runtime (NativeScript/runtime/android/{napi,jsi}/Runtime.cpp) module resolution, stack traces, MetadataNode origins, inspector

The quickjs and primjs backends keep env_to_jsr_cache in a tns::SimpleMap, which has no lock either. The hermes backend already guards its copy with env_to_jsr_mutex.

MetadataNode::s_metadata_node_cache and (with #96) ArgConverter's long-conversion cache already use tns::ConcurrentMap, which is the pattern to follow for the maps.

Expected

Creating or disposing one runtime can't corrupt state another runtime is reading. Possible fixes:

Environment

Related: #93, #96, and #97 and #98 (the console timer maps).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions