Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Fix napi_create_reference to work correctly with integers #380

Closed

Description

@kfarnung
  • Version: master
  • Platform: windows (probably others)
  • Subsystem: n-api

The N-API implementation of napi_create_reference uses JsCreateWeakReference. If a reference is created for an integer then there is an access violation during the GC update of the weak reference map. I proposed a possible fix in chakra-core/ChakraCore#3591, but once an approved fix is found we'll need to re-enable test_general/test.

Activity

  1. added a commit that references this issue on Aug 26, 2017
  2. MSLaguana commented on Sep 7, 2017

    @MSLaguana
    Contributor

    It looks like test_general/test has continued to evolve and is broken for an additional reason now: It attempts to create an object, wrap it, lose the JS reference to the object, force a GC, and ensure that the finalizer passed when wrapping was called. At the moment it looks like we might be mid-gc when the JS tries to force a gc, and so we complete that GC but it doesn't delete the only-just-now-created object.

    Other than that, there is some discussion about whether it makes sense to create references to primitive values at nodejs/node-addon-api#131

  3. gabrielschulhof commented on Sep 7, 2017

    @gabrielschulhof
  4. digitalinfinity commented on Sep 8, 2017

    @digitalinfinity
    Contributor

    @gabrielschulhof semantically, what does a reference to a primitive mean if a primitive is not heap allocated? ChakraCore's JSRT returns JsValueRefs that are opaque (can be either pointers to heap allocated objects or primitive values)- are you suggesting in the case of primitive values, we always return boxed (heap allocated) values? That seems to be a rather expensive perf cost for supporting obscure cases, no? Alternatively, if you're not suggesting that, then when you say that we create references to primitive values, are you suggesting that we box the value as part of the CreateWeakReference api? That also seems fraught since that value would get collected at the next GC since no one would have a strong handle to that, just a weak one.

    So it's unclear what the desired behavior should be. My instinct is that if napi has a chance to define this behavior more concretely right now, we should just say that napi_create_weakreference can return an error if the napi_value passed in can't be weakly referenced- that exposes some concept of VM internals but then again, so does napi_create_weakreference, and this approach allows VM vendors to not be "boxed in" 😄

  5. gabrielschulhof commented on Sep 9, 2017

    @gabrielschulhof

    I think it's pretty safe to return napi_invalid_arg for values that cannot be referenced. Any engine worth its salt should be able to provide persistent references to objects and functions. V8 is known to have features that are redundant, or have been rendered redundant by its evolution.

    We might even go so far as to document that only objects and functions can be retained as napi_refs. After all, primitive values are better stored as their native equivalents.

  6. gabrielschulhof commented on Sep 9, 2017

    @gabrielschulhof

    In fact, we document that the napi_value to which a reference is created must be an object. So, I was basically abusing the napi_create_reference() mechanism. My bad.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions