terminalService runs multiple commands in same terminal

I’m running a for loop to open multiple terminals and run different commands on each one of them.
However, only one terminal is opening and all the commands are running inside that on terminal.

Any reason of this?

These is the code i ran:

    commands.forEach(async (command) => {
                    const termWidget = await this.terminalService.newTerminal()
                    this.terminalService.open(termWidget)
                    await termWidget.start();
                    termWidget.sendText(command + "\n");
                })

@vivek9patel thank you for the discussion :+1:

I confirmed the behavior with a minimal example, it seems that the issue comes from the terminals created too quickly or simultaneously to the point where the code that either creates or retrieves a terminal thinks they were created at the same time.

You can workaround the issue by either providing your own unique id when creating the terminal (passing it as options to newTerminal) or introducing a delay between the creations.

1 Like

@vivek9patel, the issue is that you introduce concurrency into your code by using forEach, which does not wait until the promise inside is resolved. A lot of UI code in Theia does not deal well with concurrent access.

You can simply run this in a normal for loop and that should fix the issue:

for (const command of commands) {
    const termWidget = await this.terminalService.newTerminal()
    this.terminalService.open(termWidget)
    await termWidget.start();
    termWidget.sendText(command + "\n");
}
1 Like