Skip to content

NegativeArraySizeException in Hk2RequestScope.release on Jersey 3.1.11 #5990

@lbnetrom

Description

@lbnetrom

After upgrading Jersey, we observed a NegativeArraySizeException being thrown during request scope cleanup.

This happens in version 3.1.11 (3.1.10...3.1.11) in org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release, where the implementation was changed from:
new HashSet<>(store.keySet()).forEach(this::remove);
to
ArrayList<ForeignDescriptor> reverse = new ArrayList<>(store.keySet());

The new ArrayList<>(store.keySet()) constructor immediately calls store.keySet().size(), which can return a negative number if the store (a LinkedHashMap) has been corrupted due to concurrent access, resulting in the following error:

java.lang.NegativeArraySizeException: -6
    at java.base/java.util.LinkedHashMap$LinkedKeySet.toArray(LinkedHashMap.java:713)
    at java.base/java.util.ArrayList.<init>(ArrayList.java:181)
    at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release(Hk2RequestScope.java:144)

Metadata

Metadata

Assignees

No one assigned

    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