Skip to content

Conversation

@yittg
Copy link
Contributor

@yittg yittg commented Dec 23, 2021

Fixes #3776

@rdblue
Copy link
Contributor

rdblue commented Dec 30, 2021

@yittg can you please explain what this is doing and why it is the correct behavior?

@yittg
Copy link
Contributor Author

yittg commented Dec 31, 2021

@rdblue The context of thread in ThreadPool inherited from the parent thread. For the first task of one thread, the parent thread is current thread that executes the task, so the context is consistent with where the task is executed. As of the second and following tasks, the context of the existing thread running the task is still the one that started the thread, which can be different from the one that executed the task. And this can cause the context class loader issue described in #3776 .

For example:

  1. Thread[A] with Context[Ca] executes Task[T1];
  2. ThreadPool starts new Thread[P1] with Context[Ca], and running Task[T1], (OK);
  3. Thread[B] with Context[Cb] executes Task[T2];
  4. ThreadPool using Thread[P1] with Context[Ca] to run the Task[T2], (not OK);

    Context[Ca] can be different from Context[Cb] and maybe already expired now.

This change records the context that executes one task in the wrapper Runnable, and sets it to the target thread before actually running the task. This can make sure the consistency of the context.

@yittg
Copy link
Contributor Author

yittg commented Jan 7, 2022

@rdblue Do you think this make sense? Or what do you think?

@rdblue
Copy link
Contributor

rdblue commented Jan 13, 2022

@yittg, I'm skeptical that this is the right approach. The dynamic class loader allows you to specify the classloader to use. It is generally better to specify the correct one instead of relying on the context loader.

In this case, it looks like the reader used to load the file's key-value metadata doesn't have the class loader set like the reader used to scan the manifest. I would first try setting the class loader the same way.

@yittg
Copy link
Contributor Author

yittg commented Jan 13, 2022

In this case, it looks like the reader used to load the file's key-value metadata doesn't have the class loader set like the reader used to scan the manifest. I would first try setting the class loader the same way.

@rdblue It's also my first choice, FYI #3776 (comment), but it's not enough, as described in my comment, you can not pass the correct one to all cases in the dependencies.

For example, the follow code snippet in java.xml

final ServiceLoader<T> serviceLoader = ServiceLoader.load(type);

IMO, the right way is not to use the static shared thread pool. But it's hard to fix here, the shared pool is involved in too many places.

So i choose this quick fix to walk around it.

@yittg
Copy link
Contributor Author

yittg commented Jan 13, 2022

@rdblue I think it's also ok to set the correct placeholder in iceberg, i can open another PR to address it, then the issue can be fixed in those scenarios not touching the same issue in third-party dependencies.

Then we can think about how to fix it thoroughly.

What do you think?

@rdblue
Copy link
Contributor

rdblue commented Jan 14, 2022

@yittg, let's get the fix for manifest file reading in and then address the next issue. I think that we should not be relying on the context classloader when loading dynamically.

@yittg
Copy link
Contributor Author

yittg commented Jan 17, 2022

let's get the fix for manifest file reading in and then address the next issue.

@rdblue I've open #3906 for this.

@yittg yittg force-pushed the fix-ctx-class-loader branch 3 times, most recently from a0cd199 to 9f9b80c Compare January 19, 2022 06:55
@yittg yittg changed the title Sync thread context class loader with where invoked for ThreadPools Fixed context classloader of threads in ThreadPools Jan 19, 2022
@yittg yittg force-pushed the fix-ctx-class-loader branch from 9f9b80c to b1c82f1 Compare January 19, 2022 07:06
@yittg
Copy link
Contributor Author

yittg commented Jan 19, 2022

@rdblue i updated changes with fixed context class loader.

looks like ci failed accidentally,
retest ?

@yittg
Copy link
Contributor Author

yittg commented Feb 21, 2022

close this one due to prefer #4177

@yittg yittg closed this Feb 21, 2022
@yittg yittg deleted the fix-ctx-class-loader branch February 21, 2022 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flink connector thrown Trying to access closed classloader

2 participants