Skip to content

System.IO.Directory.CreateTempSubdirectory(String prefix) fails with Success exception in wasi-wasm build #92336

Open

Description

@dbaeumer

Description

Calling System.IO.Directory.CreateTempSubdirectory(String prefix) fails with an exception in a wasi-wasm build

Reproduction Steps

  • .Net version 8.0.100-rc.1.23455.8
  • tempDir.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
    <OutputType>Exe</OutputType>
    <PublishTrimmed>true</PublishTrimmed>
    <WasmSingleFileBundle>true</WasmSingleFileBundle>
  </PropertyGroup>
</Project>
  • Program.cs
using System;
using System.IO;
using System.Text;

Console.WriteLine("Hello, Wasi Console!");

using (var writer = File.Create("/tmp/test.txt")) {
	writer.Write(Encoding.UTF8.GetBytes("Hello World!"));
}

var dir = Directory.CreateTempSubdirectory();
Console.WriteLine("Created temp dir: " + dir.FullName);
  • dotnet build
  • create a tmp directory in the workspace directory
  • wasmtime 12.0.2
  • run wasmtime run --mapdir /::. --env TMPDIR=/tmp obj/Debug/net8.0/wasi-wasm/wasm/for-build/tempDir.wasm

Expected behavior

Creating the temp directory works

Actual behavior

System.IO.IOException: Success
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError)
   at Interop.ThrowIOExceptionForLastError()
   at System.IO.Directory.CreateTempSubdirectoryCore(String prefix)
   at System.IO.Directory.CreateTempSubdirectory(String prefix)
   at Program.<Main>$(String[] args)

The write to the file happened successful.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Sep 20, 2023
  2. added
    arch-wasmWebAssembly architecture
    os-wasiRelated to WASI variant of arch-wasm
    on Sep 20, 2023
  3. added this to the 9.0.0 milestone on Sep 20, 2023
  4. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Sep 20, 2023
  5. dbaeumer commented on Sep 20, 2023

    @dbaeumer
    Author

    I can create a directory as well using the following code

    var dirOne = Directory.CreateDirectory("/tmp/dirOne");
    Console.WriteLine("Created dir: " + dirOne.FullName);
  6. pavelsavara commented on Sep 20, 2023

    @pavelsavara
    Member

    Could be related #81210 and WebAssembly/wasi-libc#414

  7. dbaeumer commented on Sep 20, 2023

    @dbaeumer
    Author

    @pavelsavara I executed the WASM file on the VS Code WASI host implementation which offers nice tracing of host calls. The Directory.CreateTempSubdirectory() call doesn't result in any host calls, so I guess this fails inside some C# code and is falsely reported as a exception with an errno. There aren't any fd_readdir calls either before.

    What I did notice as well is that the wasm code does tons of clock_time_get(id: monotonic, precision: 1) calls.

  8. pavelsavara commented on Sep 20, 2023

    @pavelsavara
    Member
  9. dbaeumer commented on Sep 20, 2023

    @dbaeumer
    Author

    Any reason why we couldn't implement that in C# for WASI. I can understand why we can't use libc since they hardcode /tmp but Python on wasm-wasi has support for this since they come with their own mkdtemp implementation.

  10. pavelsavara commented on Sep 20, 2023

    @pavelsavara
    Member

    That's what #81210 wants I think.

    It would be also good to do that for emscripten build.

    Because the idea of generating randomness out of timer + checking if there is file system conflict is very ugly.
    We had issues with that already because of the granularity of browser timer used.

    https://github.com/emscripten-core/emscripten/blob/813a727fa77b3596c75a6d62b2fdb87365f9d75a/system/lib/libc/musl/src/temp/mkdtemp.c#L16-L19

    https://github.com/emscripten-core/emscripten/blob/813a727fa77b3596c75a6d62b2fdb87365f9d75a/system/lib/libc/musl/src/temp/__randname.c#L13-L14

    PR welcome.

  11. adamsitnik commented on Sep 22, 2023

    @adamsitnik
    Member

    It reminds me of

    image

  12. pavelsavara commented on Sep 22, 2023

    @pavelsavara
    Member

    SetLastError FTW ? 😎

  13. added
    blockedIssue/PR is blocked on something - see comments
    on Jul 9, 2024
  14. ilonatommy commented on Jul 9, 2024

    @ilonatommy
    Member
  15. modified the milestones: 9.0.0, Future on Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmWebAssembly architecturearea-System.IOblockedIssue/PR is blocked on something - see commentsos-wasiRelated to WASI variant of arch-wasm

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions