Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rare windows-specific bug with VSCode embedded terminal #16985

Closed
ndbn opened this issue Aug 27, 2023 · 4 comments
Closed

Rare windows-specific bug with VSCode embedded terminal #16985

ndbn opened this issue Aug 27, 2023 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@ndbn
Copy link
Contributor

ndbn commented Aug 27, 2023

Zig Version

0.12.0-dev.152+411462e1c

Steps to Reproduce and Observed Behavior

Hello. I've started learning Zig on windows, I'm using VSCode.
VSCode has embedded terminals such as PowerShell, cmd, git bash (mingw), etc.
Not sure how it was made, that might be named pipes or anything else.
It is very common situation, when you write a code in VsCode and then compile it in integrated terminal
The problem is: when Zig program has print's it can throw exception when printed in that embedded terminal.
Minimal code to reproduce

const std = @import("std");
const _print = std.debug.print;
fn print(comptime fmt: []const u8, args: anytype) !void {
    const io = std.io;
    // const stdio = io.getStdErr().writer();
    const stdio = io.getStdOut().writer();
    try stdio.print(fmt, args);
}
pub fn main() !void {
    for (0..30) |i| try print("{d}\n", .{i});
}

So when we build via zig build run in terminal (it can have 10 successful runs, but the 11th one gives exception)
(If it panics with 2 digit printing like 11 or 20 we will get panic: start index 4 is larger than end index 2)

image

writer.zig:

pub fn writeAll(self: Self, bytes: []const u8) Error!void {
            var index: usize = 0;
            while (index != bytes.len) {
                index += try self.write(bytes[index..]);
            }
        }

Here we have an index, that is incremented by number of written bytes and we get that error index > bytes.len. I understand, self.write may try to write 5 bytes of 10 (for ex.) so will return 5, which will lead to next write iteration.
I'm not sure about index != bytes.len, yes of course, in any usual situation index must not be greater than bytes.len, but I found that parcticular one.
On windows self.write goes to std/os/windows.zig into function WriteFile which is wrapper for API.

        const adjusted_len = math.cast(u32, bytes.len) orelse maxInt(u32);
        if (kernel32.WriteFile(handle, bytes.ptr, adjusted_len, &bytes_written, overlapped) == 0) {
            switch (kernel32.GetLastError()) {
                .INVALID_USER_BUFFER => return error.SystemResources,
                .NOT_ENOUGH_MEMORY => return error.SystemResources,
                .OPERATION_ABORTED => return error.OperationAborted,
                .NOT_ENOUGH_QUOTA => return error.SystemResources,
                .IO_PENDING => unreachable,
                .BROKEN_PIPE => return error.BrokenPipe,
                .INVALID_HANDLE => return error.NotOpenForWriting,
                .LOCK_VIOLATION => return error.LockViolation,
                else => |err| return unexpectedError(err),
            }
        }
        return bytes_written;

As far as I got it, kernel32.WriteFile returns not a 0, so we don't have any error.
But here the bytes_written is doubled. It only happens in integrated VSCode terminal. It repeats with zig build run and also builded exe .\zig-out\bin\test.exe, it repeats when outed in stdout and stderr and as I've seen, with out in stderr that happens more often,

Expected Behavior

Can't reproduce it with standalone cmd/Powershell, but have seen it with only 2 print's in code, Less print's = rare exceptions.

@ndbn ndbn added the bug Observed behavior contradicts documented or intended behavior label Aug 27, 2023
@mpfaff
Copy link
Contributor

mpfaff commented Aug 29, 2023

Possibly related? microsoft/terminal#40

Sounds like it was fixed though.

@ndbn
Copy link
Contributor Author

ndbn commented Aug 29, 2023

Possibly related? microsoft/terminal#40
Sounds like it was fixed though.

Seems yes. I really use Win10 before that 2018' Spring Creators Update.

@ndbn ndbn closed this as completed Aug 29, 2023
@mpfaff
Copy link
Contributor

mpfaff commented Aug 29, 2023

Seems yes. I really use Win10 before that 2018' Spring Creators Update.

I wouldn't have guessed that! Glad I decided to mention it in case it was related. I'm sure there's others out there in the same boat as you. Hopefully this issue report can help them in the future.

@ndbn
Copy link
Contributor Author

ndbn commented Aug 29, 2023

I wouldn't have guessed that! Glad I decided to mention it in case it was related. I'm sure there's others out there in the same boat as you. Hopefully this issue report can help them in the future.

Yes, thanks for that. It still rare situation, I use node.js and rust earlier in VSCode and there was no problem with output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants