Skip to content

PostgreSQL processes remain running after JabRef crashes or is forcefully terminated #12844

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

Open
2 tasks done
ungerts opened this issue Mar 28, 2025 · 10 comments · May be fixed by #12927
Open
2 tasks done

PostgreSQL processes remain running after JabRef crashes or is forcefully terminated #12844

ungerts opened this issue Mar 28, 2025 · 10 comments · May be fixed by #12927

Comments

@ungerts
Copy link
Contributor

ungerts commented Mar 28, 2025

JabRef version

Latest development branch build (please note build date below)

Operating system

Windows

Details on version and operating system

Windows 10

Checked with the latest development build (copy version output from About dialog)

  • I made a backup of my libraries before testing the latest development version.
  • I have tested the latest development version and the problem persists

Steps to reproduce the behaviour

When JabRef crashes or is forcefully terminated (e.g., via kill -9 or Task Manager), associated PostgreSQL processes often remain running. These orphaned processes continue to consume system resources unnecessarily.

Steps to Reproduce

  1. Start JabRef with a PostgreSQL backend.
  2. Forcefully terminate JabRef (e.g., using kill -9 or Task Manager).
  3. Open Task Manager (Windows) or use ps on Linux/macOS.
  4. Observe that multiple PostgreSQL Server processes are still running.

Expected Behavior

All PostgreSQL-related processes spawned by JabRef should terminate when JabRef is unexpectedly closed.

Actual Behavior

PostgreSQL processes persist after JabRef exits unexpectedly, leading to orphaned background processes.

Appendix

Screenshot

PostgreSQL orphaned processes

Screenshot shows several idle PostgreSQL Server processes still running after JabRef was terminated.

@github-project-automation github-project-automation bot moved this to Normal priority in Prioritization Mar 28, 2025
@Siedlerchr
Copy link
Member

Sigkill cannot be caught, but it would probably make sense to add a shutdown hook to ensure running instances are closed when sigterm occurs

@subhramit
Copy link
Member

subhramit commented Mar 28, 2025

I have faced this too (noticed this yesterday). Every time I close and re-run JabRef (when running via IntelliJ), my task manager shows the old Postgres instance running. On running a new JabRef session again, a new Postgres session is also launched. After 5-6 trials, I was wondering why my system was lagging so much, then checked the task manager which had 6 running postgres instances.

Edit - what I meant was another implication of this is lag during development.

Edit - more details:
1st run: 1 background process, 6 instances:

Image

Second run using IntelliJ reload:

Image

Now there are 2 background processes, 12 instances:

Image

All of them on CPU 00:

Image

And so on...

@subhramit subhramit moved this from Normal priority to High priority in Prioritization Mar 28, 2025
@subhramit subhramit added this to the 6.0-beta milestone Mar 28, 2025
@Siedlerchr
Copy link
Member

Siedlerchr commented Mar 28, 2025

@subhramit
Copy link
Member

Tried with:

public class Launcher {

    public static void main(String[] args) {
       ...

        PostgreServer postgreServer = new PostgreServer();
        Injector.setModelOrService(PostgreServer.class, postgreServer);
        
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            if (postgreServer != null) {
                try {
                    postgreServer.shutdown();
                    System.out.println("PostgreServer successfully shut down");
                } catch (Exception e) {
                    System.err.println("Error shutting down PostgreServer: " + e.getMessage());
                }
            }
        }));

        JabRefGUI.setup(uiCommands, preferences, fileUpdateMonitor);
        JabRefGUI.launch(JabRefGUI.class, args);
    }
}

Didn't work

@subhramit
Copy link
Member

Tried with:
...
Didn't work

On checking logs, neither the try nor the catch is executed. On reading more, it appears that it's because there is no way to reliably execute code in case of a SIGKILL (the shutdownhook helps in case of SIGTERM and SIGINT).

@Siedlerchr
Copy link
Member

Siedlerchr commented Mar 28, 2025 via email

@subhramit
Copy link
Member

Hmm...so is this issue unsolvable from the perspective of shutdown it seems..

@koppor
Copy link
Member

koppor commented Mar 29, 2025

Hmm...so is this issue unsolvable from the perspective of shutdown it seems..

Idea for workaround: On start of new JabRef instance, old postgres (from JabRef) could be re-used (or killed)

@ungerts
Copy link
Contributor Author

ungerts commented Mar 29, 2025

Idea for workaround: On start of new JabRef instance, old postgres (from JabRef) could be re-used (or killed)

To me, this feels a bit like performing "magic" on the user's machine. Implementing such behavior is likely to be complex and error-prone.

  • PostgreSQL ports are allocated automatically. As a result, the application would need to either:

    • Detect the port currently in use at runtime, or
    • Persist the assigned port on first launch and reuse it upon restart.
  • Multiple PostgreSQL instances might be running on the user's system:

    • Some could be installed manually by the user.
    • Others might be leftover (or orphaned) instances from previous JabRef sessions.

This leads to a critical question: Which instance should JabRef attempt to reuse - or safely shut down?

@subhramit subhramit modified the milestones: 6.0-beta, 6.0 Mar 30, 2025
@UdayHE
Copy link

UdayHE commented Apr 12, 2025

On start of new JabRef instance, old postgres (from JabRef) could be re-used (or killed)

what if we do like this?

1.Record Postgres Metadata on Start

When starting Embedded Postgres:
Write its PID (if available) or the port it’s using into a file (e.g., /tmp/jabref-postgres-info.json)
Include the Java process ID (pid) and maybe a startup timestamp
below is the JSON

{
  "pid": 12345,
  "port": 54321,
  "startedBy": "jabref",
  "startedAt": "2025-04-12T18:40:00Z"
}

2.Check on New Start:

On new Instance startup:
Check if /tmp/jabref-postgres-info.json exists
If so:
Check if that PID is alive and if the port is still open
If it is alive → reuse
If not alive or broken → delete file and start new, possibly killing it.

UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
…ination (Fix JabRef#12844)

* Added logic to detect and clean up stale embedded PostgreSQL instances left behind by previous JabRef sessions.

* Introduced PostgresProcessCleaner to safely shut down any orphaned PostgreSQL processes using PID-based detection.

* Registered a shutdown hook in Launcher to ensure embedded PostgreSQL is properly terminated during normal or abrupt shutdown.
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
…ination (Fix JabRef#12844)

* Added logic to detect and clean up stale embedded PostgreSQL instances left behind by previous JabRef sessions.

* Introduced PostgresProcessCleaner to safely shut down any orphaned PostgreSQL processes using PID-based detection.

* Registered a shutdown hook in Launcher to ensure embedded PostgreSQL is properly terminated during normal or abrupt shutdown.

* Updated CHANGELOG.md
@UdayHE UdayHE linked a pull request Apr 13, 2025 that will close this issue
7 tasks
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
…ination (Fix JabRef#12844)

* Added logic to detect and clean up stale embedded PostgreSQL instances left behind by previous JabRef sessions.

* Introduced PostgresProcessCleaner to safely shut down any orphaned PostgreSQL processes using PID-based detection.

* Registered a shutdown hook in Launcher to ensure embedded PostgreSQL is properly terminated during normal or abrupt shutdown.
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
…ination (Fix JabRef#12844)

* Added logic to detect and clean up stale embedded PostgreSQL instances left behind by previous JabRef sessions.

* Introduced PostgresProcessCleaner to safely shut down any orphaned PostgreSQL processes using PID-based detection.

* Registered a shutdown hook in Launcher to ensure embedded PostgreSQL is properly terminated during normal or abrupt shutdown.

* Updated CHANGELOG.md
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
…ination (Fix JabRef#12844)

* Added logic to detect and clean up stale embedded PostgreSQL instances left behind by previous JabRef sessions.

* Introduced PostgresProcessCleaner to safely shut down any orphaned PostgreSQL processes using PID-based detection.

* Registered a shutdown hook in Launcher to ensure embedded PostgreSQL is properly terminated during normal or abrupt shutdown.

* Updated CHANGELOG.md
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
…ination (Fix JabRef#12844)

* style changes in PostgreProcessCleaner, Launcher
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
…ination (Fix JabRef#12844)

* style changes in PostgreServer imports
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 13, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
…ination (Fix JabRef#12844)

* Modified PostgreProcessCleaner logic to fix windows os issue.
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
…ination (Fix JabRef#12844)

* Used Path.of() instead of Paths.get() in PostgreProcessCleaner.
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
* Fix for trag-bot comments.
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
* Fix for trag-bot comments.
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 15, 2025
* Fix for trag-bot comments.
* Created separate class PostgresMetadataWriter to follow SRP.
UdayHE added a commit to UdayHE/jabref that referenced this issue Apr 16, 2025
* Fix for trag-bot comments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: High priority
Development

Successfully merging a pull request may close this issue.

5 participants