Giter Club home page Giter Club logo

Comments (7)

segrey avatar segrey commented on September 3, 2024

Sorry for the delay. You probably need to investigate how to properly destroy the underlying process.

But with a continuous process like top, a background process keeps running.

Could you please provide more details what you mean that a background process keeps running? For me, process.destroyForcibly() sends SIGKILL on Unix, so the top process is terminated.

from jediterm.

manticore-projects avatar manticore-projects commented on September 3, 2024

Let me describe my observation in detail:

  1. opening a dialog with Jediterm inside (but without executing any shell command), then closing the dialog works and the whole swing application closes.

  2. same as above, but executing a shell command, which terminates/exits (e. g. ls, pwd, echo ...) and then closing the dialog leaves the swing application open (the dialog closes but not all objects get destroyed).

  3. same as above, but with a continuously running shell command ( e. g. top) also prevents the swing application from closing.

I would understand use case 3, but even use case 2 prevents the swing application from exit?!
Even an exit command into the shell before closing will not help.

from jediterm.

segrey avatar segrey commented on September 3, 2024

Thanks for clarification. Unfortunately, it is not reproduced for me on macOS using the steps:

  1. Clone this repo
  2. Replace JediTermMain.kt#L81-L82 with a single line
        arrayOf("top")
  1. Run the application: ./gradlew :JediTerm:run. A Swing application is opened with top command inside.
  2. Close the application frame with mouse. The application is closed as expected.

If it are observing a different behavior, could you please provide a small example project with details how to reproduce it locally?

from jediterm.

manticore-projects avatar manticore-projects commented on September 3, 2024

If it are observing a different behavior, could you please provide a small example project with details how to reproduce it locally?

Sorry for late response, please see below.
When closing without calling a OS command, the Application terminates as expected. But when closing after calling any OS command (e. g. ls or top) it does not terminate and stays with a thread running.

package com.manticore.ui;

import com.jediterm.pty.PtyProcessTtyConnector;
import com.jediterm.terminal.TtyConnector;
import com.jediterm.terminal.ui.JediTermWidget;
import com.jediterm.terminal.ui.TerminalWidget;
import com.jediterm.terminal.ui.TerminalWidgetListener;
import com.jediterm.terminal.ui.UIUtil;
import com.jediterm.terminal.ui.settings.DefaultSettingsProvider;
import com.manticore.swingui.MDialog;
import com.pty4j.PtyProcess;
import com.pty4j.PtyProcessBuilder;
import org.junit.jupiter.api.Test;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

class AdminPanelTest {
    public final static Logger LOGGER = Logger.getLogger(AdminPanelTest.class.getName());

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Map<String, String> envs = System.getenv();
                    String[] command;
                    if (UIUtil.isWindows) {
                        command = new String[] {"cmd.exe"};
                    } else {
                        command = new String[] {"/bin/sh", "--login"};
                        envs = new HashMap<>(System.getenv());
                        envs.put("TERM", "xterm-256color");
                    }
                    PtyProcess process =
                            new PtyProcessBuilder().setCommand(command).setEnvironment(envs).start();
                    TtyConnector connector = new PtyProcessTtyConnector(process, StandardCharsets.UTF_8);

                    JediTermWidget widget = new JediTermWidget(80, 24, new DefaultSettingsProvider());
                    widget.setTtyConnector(connector);
                    widget.start();

                    JDialog dialog = new JDialog();
                    dialog.add(widget);

                    dialog.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosing(WindowEvent e) {
                            if (widget.isSessionRunning()) {
                                widget.stop();
                                widget.close();
                            }
                            if (connector.isConnected()) {
                                connector.close();
                            }
                            if (process.isAlive()) {
                                process.destroyForcibly();
                            }
                            if (dialog.isShowing()) {
                                dialog.setVisible(false);
                            }
                        }
                    });

                    widget.addListener(new TerminalWidgetListener() {
                        @Override
                        public void allSessionsClosed(TerminalWidget terminalWidget) {
                            if (widget.isSessionRunning()) {
                                widget.stop();
                                widget.close();
                            }
                            if (connector.isConnected()) {
                                connector.close();
                            }
                            if (process.isAlive()) {
                                process.destroyForcibly();
                            }
                            if (dialog.isShowing()) {
                                dialog.setVisible(false);
                            }
                        }
                    });
                    dialog.setDefaultCloseOperation(MDialog.DISPOSE_ON_CLOSE);
                    dialog.setModal(false);
                    dialog.pack();
                    dialog.setVisible(true);
                } catch (Exception ex) {
                    LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
                }
            }
        });
    }
}

from jediterm.

manticore-projects avatar manticore-projects commented on September 3, 2024

4. Close the application frame with mouse. The application is closed as expected.

I believe, this is achieved by forcefully setting the Frame.EXIT_ON_CLOSE which is shading the underlying problem of a rouge thread kept running somewhere.

Of course, I could always call System.exit() -- but that is not desirable in a Multi-User/Application Server scenario.

from jediterm.

segrey avatar segrey commented on September 3, 2024

@manticore-projects Thanks, reproduced. Fixed by a7670d9. Please see the updated BasicTerminalShellExample.java. Now JVM should exit gracefully on closing the window with mouse or on exiting the terminal session (Ctrl+D or exit).

from jediterm.

manticore-projects avatar manticore-projects commented on September 3, 2024

Thank you so much for looking into and fixing it. I do appreciate.
Cheers.

from jediterm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.