corgy-w opened a new pull request, #13367:
URL: https://github.com/apache/seatunnel/pull/13367
### What
`AbstractPluginDiscovery.createOptionalPluginInstance` builds a `URL[]` with
one slot too many before constructing the fallback `URLClassLoader`:
```java
URL[] urls = new URL[pluginJars.size() + 1];
int i = 0;
for (URL pluginJar : pluginJars) {
urls[i++] = pluginJar;
}
urls = Stream.concat(Arrays.stream(urls),
pluginJarPaths.get().stream()).distinct().toArray(URL[]::new);
classLoader = new URLClassLoader(urls,
Thread.currentThread().getContextClassLoader());
```
The extra slot is never filled, `distinct()`/`toArray()` keep the `null`,
and `URLClassLoader`'s internal `URLClassPath` adds every URL to an
`ArrayDeque`, which rejects `null`, so the constructor always throws.
This is not an edge case on JDK 9+: `AppClassLoader` is not a
`URLClassLoader`, so the default `DEFAULT_URL_TO_CLASSLOADER` consumer throws
`UnsupportedOperationException`, meaning the catch block is always reached and
the fallback always NPEs.
Verified on JDK 11 with the buggy code:
```
java.lang.NullPointerException
at java.base/java.util.ArrayDeque.addLast(ArrayDeque.java:304)
at java.base/java.util.ArrayDeque.add(ArrayDeque.java:495)
at
java.base/jdk.internal.loader.URLClassPath.<init>(URLClassPath.java:155)
at
java.base/jdk.internal.loader.URLClassPath.<init>(URLClassPath.java:172)
at java.base/java.net.URLClassLoader.<init>(URLClassLoader.java:120)
at
org.apache.seatunnel.plugin.discovery.AbstractPluginDiscovery.createOptionalPluginInstance(AbstractPluginDiscovery.java:249)
```
### Fix
Build the array directly from the two streams, so no `null` entry can appear:
```java
URL[] urls =
Stream.concat(additionalPluginJars.stream(),
pluginJarPaths.get().stream())
.distinct()
.toArray(URL[]::new);
classLoader = new URLClassLoader(urls,
Thread.currentThread().getContextClassLoader());
```
Everything else (logging, ordering, the `loadPluginInstance` call) is
unchanged.
### Rename (no behaviour change)
`pluginJars` was ambiguous because the class also has the discovered
`pluginJarPaths`: the parameter actually means *extra jars the caller passes
in*. It is renamed to `additionalPluginJars` in:
- `PluginDiscovery` (both interface methods, with the javadoc now saying
these are additional jars used to help the plugin load, as opposed to the jars
discovered from the plugin directory),
- `AbstractPluginDiscovery` (the override, the internal `(List<URL>)` cast
and `createPluginInstance(...)`, which forwards its own parameter).
`git grep createOptionalPluginInstance` across the tree shows
`AbstractPluginDiscovery` is the only implementation. No method name, signature
or type is changed; parameter names are not part of the API/binary contract, so
this is source- and binary-compatible.
### Test
`AbstractPluginDiscoveryTest#testCreateOptionalPluginInstanceFallbackWithoutUrlClassLoader`
(added to the existing test class, no new test class):
- builds a plugin dir containing a `connector-fallback.jar`, plus an
additional jar that carries a `META-INF/services` descriptor for a test plugin,
- sets the thread context classloader to a plain `ClassLoader` (not a
`URLClassLoader`) delegating to the test classloader, so the default consumer
throws and the catch block executes,
- asserts the plugin instance is still created by the fallback
`URLClassLoader` and is the expected implementation.
Red (old implementation restored, `-DtrimStackTrace=false`): the new test
errors with the `URLClassPath.<init>` NPE quoted above.
Green after the fix, `./mvnw -o -pl seatunnel-plugin-discovery test
-Dskip.spotless=true`:
```
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.307 s - in
org.apache.seatunnel.plugin.discovery.AbstractPluginDiscoveryTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 s - in
org.apache.seatunnel.plugin.discovery.seatunnel.SeaTunnelSourcePluginDiscoveryTest
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS
```
`./mvnw -o -q -pl seatunnel-plugin-discovery spotless:apply` was run before
the final test run.
### Compatibility
No public API, signature, method name or type changes; only the fallback
array construction is fixed and a parameter name is clarified.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]