Skip to content

forkpty() / execvpe() causing Segmentation fault under Ubuntu Linux 22.04.1 LTS (.Net 7.0.3) #32

@ylmw0131

Description

@ylmw0131

Entry call:

using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Pty.Net;

namespace TestApp
{
    internal class Application
    {
        static void Main(string[] args)
        {
            var app = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Path.Combine(Environment.SystemDirectory, "cmd.exe") : "sh";

            var options = new PtyOptions
            {
                Name = "Custom terminal",
                Cols = 80,
                Rows = 25,
                Cwd = Environment.CurrentDirectory,
                App = app,
                Environment = new Dictionary<string, string>()
                              {
                                { "FOO", "bar" },
                                { "Bazz", string.Empty },
                              },
            };

            PtyProvider.SpawnAsync(options, new CancellationToken());

            Task.Delay(-1).Wait();
        }
    }
}

Segmentation fault happened calling forkpty() / execvpe()

Pty.Net\Linux\PtyProvider.cs

int controller = 0;
int pid = forkpty(ref controller, null, ref term, ref winSize);

if (pid == -1)
{
    throw new InvalidOperationException($"forkpty(4) failed with error {Marshal.GetLastWin32Error()}");
}

if (pid == 0)
{
    // We are in a forked process! See http://man7.org/linux/man-pages/man2/fork.2.html for details.
    // Only our thread is running. We inherited open file descriptors and get a copy of the parent process memory.
    Environment.CurrentDirectory = options.Cwd;
    execvpe(options.App, terminalArgs, options.Environment);

    // Unreachable code after execvpe()
}

**

I have tested for .Net 6, it works.

Wonding whether it related to the large VSZ (Virtual Memory Size) on .Net 7 / .Net 8 which lead to forking
Segmentation fault issue.

**

NetSDK: .Net 7.0.3
Application Type: Console
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS x64
Release: 22.04
Codename: jammy

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions