forked from qt-creator/qt-creator
Terminal: Fix process staying alive
When starting a Terminal Process with TerminalMode::Detached, the Process object was not deleted on app exit and therefor created warnings on exit. This patch changes it so that the osascript does not wait for the process to exit and therefor exits pretty much immediately which deletes the Process object. Fixes: QTCREATORBUG-29375 Change-Id: I08e8b753c98011fdafc32f03886b5c0be7aec801 Reviewed-by: Eike Ziller <eike.ziller@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -21,7 +21,7 @@ ProcessStubCreator::ProcessStubCreator(TerminalInterface *interface)
|
|||||||
: m_interface(interface)
|
: m_interface(interface)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
static const QLatin1String TerminalAppScript{R"(
|
static const QLatin1String TerminalAppScriptAttached{R"(
|
||||||
tell application "Terminal"
|
tell application "Terminal"
|
||||||
activate
|
activate
|
||||||
set newTab to do script "%1 && exit"
|
set newTab to do script "%1 && exit"
|
||||||
@@ -33,14 +33,27 @@ static const QLatin1String TerminalAppScript{R"(
|
|||||||
end tell
|
end tell
|
||||||
)"};
|
)"};
|
||||||
|
|
||||||
|
static const QLatin1String TerminalAppScriptDetached{R"(
|
||||||
|
tell application "Terminal"
|
||||||
|
activate
|
||||||
|
do script "%1 && exit"
|
||||||
|
end tell
|
||||||
|
)"};
|
||||||
|
|
||||||
|
struct AppScript
|
||||||
|
{
|
||||||
|
QString attached;
|
||||||
|
QString detached;
|
||||||
|
};
|
||||||
|
|
||||||
expected_str<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData &setupData)
|
expected_str<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData &setupData)
|
||||||
{
|
{
|
||||||
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
|
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
|
||||||
bool detached = setupData.m_terminalMode == TerminalMode::Detached;
|
bool detached = setupData.m_terminalMode == TerminalMode::Detached;
|
||||||
|
|
||||||
if (HostOsInfo::isMacHost()) {
|
if (HostOsInfo::isMacHost()) {
|
||||||
static const QMap<QString, QString> terminalMap = {
|
static const QMap<QString, AppScript> terminalMap = {
|
||||||
{"Terminal.app", TerminalAppScript},
|
{"Terminal.app", {TerminalAppScriptAttached, TerminalAppScriptDetached}},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (terminalMap.contains(terminal.command.toString())) {
|
if (terminalMap.contains(terminal.command.toString())) {
|
||||||
@@ -48,29 +61,36 @@ expected_str<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData
|
|||||||
= Utils::transform(setupData.m_environment.toStringList(), [](const QString &env) {
|
= Utils::transform(setupData.m_environment.toStringList(), [](const QString &env) {
|
||||||
return CommandLine{"export", {env}}.toUserOutput();
|
return CommandLine{"export", {env}}.toUserOutput();
|
||||||
}).join('\n');
|
}).join('\n');
|
||||||
const QString shScript = QString("cd '%1'\n%2\nclear\n'%3' %4\n")
|
|
||||||
|
Process *process = new Process(detached ? nullptr : this);
|
||||||
|
if (detached) {
|
||||||
|
QObject::connect(process, &Process::done, process, &Process::deleteLater);
|
||||||
|
}
|
||||||
|
|
||||||
|
QTemporaryFile shFile;
|
||||||
|
shFile.setAutoRemove(false);
|
||||||
|
QTC_ASSERT(shFile.open(),
|
||||||
|
return make_unexpected(Tr::tr("Failed to open temporary script file.")));
|
||||||
|
|
||||||
|
const QString shScript = QString("cd '%1'\n%2\nclear\n'%3' %4\nrm '%5'\n")
|
||||||
.arg(setupData.m_workingDirectory.nativePath())
|
.arg(setupData.m_workingDirectory.nativePath())
|
||||||
.arg(env)
|
.arg(env)
|
||||||
.arg(setupData.m_commandLine.executable().nativePath())
|
.arg(setupData.m_commandLine.executable().nativePath())
|
||||||
.arg(setupData.m_commandLine.arguments());
|
.arg(setupData.m_commandLine.arguments())
|
||||||
|
.arg(shFile.fileName());
|
||||||
|
|
||||||
Process *process = new Process(detached ? nullptr : this);
|
shFile.write(shScript.toUtf8());
|
||||||
if (detached)
|
shFile.close();
|
||||||
QObject::connect(process, &Process::done, process, &Process::deleteLater);
|
|
||||||
|
|
||||||
QTemporaryFile *shFile = new QTemporaryFile(process);
|
FilePath::fromUserInput(shFile.fileName())
|
||||||
QTC_ASSERT(shFile->open(),
|
|
||||||
return make_unexpected(Tr::tr("Failed to open temporary script file.")));
|
|
||||||
shFile->write(shScript.toUtf8());
|
|
||||||
shFile->close();
|
|
||||||
|
|
||||||
FilePath::fromUserInput(shFile->fileName())
|
|
||||||
.setPermissions(QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther | QFile::ReadUser
|
.setPermissions(QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther | QFile::ReadUser
|
||||||
| QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser
|
| QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser
|
||||||
| QFile::WriteGroup | QFile::WriteOther);
|
| QFile::WriteGroup | QFile::WriteOther);
|
||||||
|
|
||||||
const QString script
|
const QString script = (detached
|
||||||
= terminalMap.value(terminal.command.toString()).arg(shFile->fileName());
|
? terminalMap.value(terminal.command.toString()).detached
|
||||||
|
: terminalMap.value(terminal.command.toString()).attached)
|
||||||
|
.arg(shFile.fileName());
|
||||||
|
|
||||||
process->setCommand({"osascript", {"-"}});
|
process->setCommand({"osascript", {"-"}});
|
||||||
process->setWriteData(script.toUtf8());
|
process->setWriteData(script.toUtf8());
|
||||||
|
|||||||
Reference in New Issue
Block a user