Aleksander Vrcko created AVRO-4362:
--------------------------------------

             Summary: Schema.VALIDATE_NAMES leaves a thread-local entry that 
keeps Avro's classloader reachable
                 Key: AVRO-4362
                 URL: https://issues.apache.org/jira/browse/AVRO-4362
             Project: Apache Avro
          Issue Type: Bug
          Components: java
    Affects Versions: 1.12.2
            Reporter: Aleksander Vrcko


{{Schema.VALIDATE_NAMES}} is a static {{ThreadLocalWithInitial}} whose initial 
value is {{NameValidator.UTF_VALIDATOR}}. Any path that validates a name gives 
the calling thread an entry, and nothing removes it. Two such paths:

* {{Schema.Parser.parse}}, which sets the value and then restores it;
* building a named schema, for example {{Schema.createRecord}}.

The entry's value is an Avro class, so it keeps Avro's classloader reachable. 
Through that classloader it also keeps the static that holds the 
{{ThreadLocal}} key, so the key never becomes stale and the entry is never 
expunged.

On any long-lived thread that outlives an Avro classloader, the whole 
classloader stays in memory. That happens with:

* servlet-container worker threads across redeployments;
* a JUnit thread across a test framework's per-test classloaders;
* development-mode reloads.

Seen on 1.12.2; the same field is on {{main}} at 08d2fd7828.

h3. Reproducer

https://github.com/Traxus0/quarkus-test-retention-reproducers/tree/avro-validate-names-threadlocal-1/avro-validate-names-threadlocal

A plain Java program, with no framework. It loads Avro into a fresh 
{{URLClassLoader}} whose parent is the platform loader, calls 
{{Schema.createRecord}} with one field on a chosen thread, discards the loader, 
and checks whether it can be collected. Run it from the repository root with 
{{./gradlew -p avro-validate-names-threadlocal run}}. On Avro 1.12.2, JDK 
25.0.2, every run gave identical output:

{noformat}
A  validate on this long-lived thread      classloader STILL REACHABLE; this 
thread's entries from it before discarding: [org.apache.avro.NameValidator$2]
B  validate on a thread that then ends     classloader COLLECTED; this thread's 
entries from it before discarding: none
C  as A, then VALIDATE_NAMES.remove()      classloader COLLECTED; this thread's 
entries from it before discarding: none
{noformat}

C shows that {{VALIDATE_NAMES}} alone holds the loader: no other thread-local, 
static or registration does.

h3. A possible direction, which needs validation

Don't populate the thread-local with the default. For example, use a plain 
{{ThreadLocal}} that is {{null}} unless a parser sets a validator, and read it 
as {{v == null ? UTF_VALIDATOR : v}}. In the common case that leaves no entry, 
and it needs no new API, which is narrower than AVRO-1595's request. We have 
not checked it against Avro's tests, and two things would have to stay right:

* *Nested parsing.* {{Parser.parse}} sets the validator and restores the 
previous one, and one parse can run inside another on the same thread. The 
restore must stay exact: an inner parse may {{remove()}} only where it found no 
value, and must otherwise put back the outer parse's validator.
* *Custom validators.* A {{NameValidator}} given through the parser must still 
be the one in force for that parse and every nested one, and must not outlive 
it on the thread.

h3. Related

AVRO-1595, "Give access to remove threadlocal values" (open since 2014). It 
asks for a way to clear Avro's thread-locals and does not mention this field.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to