Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Portal.close for java_thread_pg_entry=allow mode #476

Merged
merged 3 commits into from
Mar 25, 2024

Conversation

jcflack
Copy link
Contributor

@jcflack jcflack commented Dec 22, 2023

The java_thread_pg_entry=allow mode is the one longest supported in PL/Java, and it ought eventually to be phased out, as its way of serializing threads' access to PG is fussy, and involves quite unconvincing manipulation of PG internal variables used for detecting excessive stack use. A better mode, not yet implemented, would be to forever allow only the original PG thread to enter PG; on entry to PL/Java, it should dispatch the Java work quickly onto some other thread and then wait as a single-thread executor service to handle doInPG requests, until completion of the thread handling the work.

However, until that better mode is implemented, allow mode ought to be usable. Issue #471 identified a bug in Portal.close causing it to fail depending on the thread invoking it.

Although any such use of Portal.close could be affected, the issue was reported in Java class loading. A library that uses threads may need to enter PL/Java's classloader on an arbitrary thread; if that doesn't work, there really aren't ways to restructure the application as a workaround that aren't impractically onerous.

The failure also gave unhelpful diagnostics, in which the triggering event (failure of a PG stack depth check) would be caught and unreported, leaving a later entry into PG to fail with "An attempt was made to call a PostgreSQL backend function after an elog(ERROR)", and that would be the mysterious cause of a ClassNotFoundException.

This PR fixes what seems to be the only instance of the buggy coding pattern.

Mechanically rewriting synchronized(THREADLOCK) as doInPG
in c3f833b was a little too mechanical where releaseFromJava
is involved. None of those DualState resource-releasing methods
bother with the stack-base-fudging business, and that isn't a bug:
they are not supposed to need to. DualState is already designed
to enqueue those actions for later when the thread is known
to be right. In allow mode, the doInPG simply fools it into
performing the action immediately.

Addresses issue #471.
As the ExecutionPlan reference is only held in Portal to make sure
it stays live as long as the Portal does, simply move the reference
into the State. That way, there is no need to bother with an extra
reference to the Portal instance just to null the plan reference;
that can be handled within the State itself.
@jcflack jcflack merged commit 02b2421 into REL1_6_STABLE Mar 25, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant