Skip to content

Commit d28627e

Browse files
fix(terminal): delay task command until shell is ready
Writing to the PTY immediately after start() was a race condition — the shell hadn't finished initializing, so the command was lost or misinterpreted. Waiting for the first PTY output ensures the shell is ready to accept input. Also switches the line ending from `\n` to `\r`, which is correct for PTY raw mode.
1 parent 06ce084 commit d28627e

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/docks/TerminalDock.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ void TerminalDock::init(const QString &shell, const QString &cwd)
7575
m_terminal->start(shell, cwd);
7676

7777
if (!m_taskCommand.isEmpty()) {
78-
QByteArray cmd = m_taskCommand.toUtf8();
79-
cmd.append('\n');
80-
m_terminal->writeToPty(cmd);
78+
connect(m_terminal, &TerminalWidget::firstOutputReceived, this, [this]() {
79+
QByteArray cmd = m_taskCommand.toUtf8();
80+
cmd.append('\r');
81+
m_terminal->writeToPty(cmd);
82+
}, Qt::SingleShotConnection);
8183
}
8284
}
8385

@@ -119,9 +121,11 @@ void TerminalDock::restartTask()
119121

120122
m_terminal->start(m_shell, m_initialCwd);
121123

122-
QByteArray cmd = m_taskCommand.toUtf8();
123-
cmd.append('\n');
124-
m_terminal->writeToPty(cmd);
124+
connect(m_terminal, &TerminalWidget::firstOutputReceived, this, [this]() {
125+
QByteArray cmd = m_taskCommand.toUtf8();
126+
cmd.append('\r');
127+
m_terminal->writeToPty(cmd);
128+
}, Qt::SingleShotConnection);
125129

126130
m_terminal->setFocus();
127131
}

src/widgets/TerminalWidget.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ void TerminalWidget::onPtyReadyRead()
434434
if (!m_pty || !m_vt) return;
435435
const QByteArray data = m_pty->readAll();
436436
if (data.isEmpty()) return;
437+
if (!m_firstOutputEmitted) {
438+
m_firstOutputEmitted = true;
439+
emit firstOutputReceived();
440+
}
437441
m_pendingInput.append(data);
438442
if (!m_batchTimer->isActive()) {
439443
m_batchTimer->start();

src/widgets/TerminalWidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TerminalWidget : public QAbstractScrollArea
6060
void titleChanged(const QString &title);
6161
void processExited(int exitCode);
6262
void spawnFailed(const QString &message);
63+
void firstOutputReceived();
6364

6465
protected:
6566
bool event(QEvent *event) override;
@@ -139,6 +140,7 @@ private slots:
139140
bool m_focusReporting = false;
140141
bool m_exited = false;
141142
int m_exitCode = 0;
143+
bool m_firstOutputEmitted = false;
142144

143145
QByteArray m_pendingInput;
144146
QTimer *m_batchTimer = nullptr;

0 commit comments

Comments
 (0)