Skip to content

Commit fcef7c4

Browse files
committed
fix std.io.InStream for windows
now we handle PIPE_BROKEN as an EOF also set up framework for debugging unexpected posix/windows errors
1 parent bb169a7 commit fcef7c4

6 files changed

Lines changed: 95 additions & 58 deletions

File tree

std/debug.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ error MissingDebugInfo;
1111
error InvalidDebugInfo;
1212
error UnsupportedDebugInfo;
1313

14+
1415
pub fn assert(ok: bool) {
1516
if (!ok) {
1617
// In ReleaseFast test mode, we still want assert(false) to crash, so

std/io.zig

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,12 @@ pub var stderr = OutStream {
4545
/// bug in the program that called the function.
4646
error Invalid;
4747

48-
/// When an Unexpected error occurs, code that emitted the error likely needs
49-
/// a patch to recognize the unexpected case so that it can handle it and emit
50-
/// a more specific error.
51-
error Unexpected;
52-
5348
error DiskQuota;
5449
error FileTooBig;
5550
error Io;
5651
error NoSpaceLeft;
5752
error BadPerm;
58-
error PipeFail;
53+
error BrokenPipe;
5954
error BadFd;
6055
error IsDir;
6156
error NotDir;
@@ -207,7 +202,10 @@ pub const OutStream = struct {
207202
if (self.handle) |handle| return handle;
208203
if (system.GetStdHandle(self.handle_id)) |handle| {
209204
if (handle == system.INVALID_HANDLE_VALUE) {
210-
return error.Unexpected;
205+
const err = system.GetLastError();
206+
return switch (err) {
207+
else => os.unexpectedErrorWindows(err),
208+
};
211209
}
212210
self.handle = handle;
213211
return handle;
@@ -292,7 +290,7 @@ pub const InStream = struct {
292290
system.EFAULT => unreachable,
293291
system.EBADF => return error.BadFd,
294292
system.EIO => return error.Io,
295-
else => return error.Unexpected,
293+
else => return os.unexpectedErrorPosix(read_err),
296294
}
297295
}
298296
if (amt_read == 0) return index;
@@ -309,12 +307,12 @@ pub const InStream = struct {
309307
const err = system.GetLastError();
310308
return switch (err) {
311309
system.ERROR.OPERATION_ABORTED => continue,
312-
system.ERROR.BROKEN_PIPE => error.PipeFail,
313-
else => error.Unexpected,
310+
system.ERROR.BROKEN_PIPE => return index,
311+
else => os.unexpectedErrorWindows(err),
314312
};
315313
}
314+
if (amt_read == 0) return index;
316315
index += amt_read;
317-
if (amt_read < want_read_count) return index;
318316
}
319317
return index;
320318
} else {
@@ -374,7 +372,7 @@ pub const InStream = struct {
374372
system.EOVERFLOW => error.Unseekable,
375373
system.ESPIPE => error.Unseekable,
376374
system.ENXIO => error.Unseekable,
377-
else => error.Unexpected,
375+
else => os.unexpectedErrorPosix(err),
378376
};
379377
}
380378
},
@@ -394,7 +392,7 @@ pub const InStream = struct {
394392
system.EOVERFLOW => error.Unseekable,
395393
system.ESPIPE => error.Unseekable,
396394
system.ENXIO => error.Unseekable,
397-
else => error.Unexpected,
395+
else => os.unexpectedErrorPosix(err),
398396
};
399397
}
400398
},
@@ -414,7 +412,7 @@ pub const InStream = struct {
414412
system.EOVERFLOW => error.Unseekable,
415413
system.ESPIPE => error.Unseekable,
416414
system.ENXIO => error.Unseekable,
417-
else => error.Unexpected,
415+
else => os.unexpectedErrorPosix(err),
418416
};
419417
}
420418
return result;
@@ -430,7 +428,7 @@ pub const InStream = struct {
430428
return switch (err) {
431429
system.EBADF => error.BadFd,
432430
system.ENOMEM => error.OutOfMemory,
433-
else => error.Unexpected,
431+
else => os.unexpectedErrorPosix(err),
434432
}
435433
}
436434

@@ -485,7 +483,10 @@ pub const InStream = struct {
485483
if (self.handle) |handle| return handle;
486484
if (system.GetStdHandle(self.handle_id)) |handle| {
487485
if (handle == system.INVALID_HANDLE_VALUE) {
488-
return error.Unexpected;
486+
const err = system.GetLastError();
487+
return switch (err) {
488+
else => os.unexpectedErrorWindows(err),
489+
};
489490
}
490491
self.handle = handle;
491492
return handle;

std/net.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const assert = @import("debug.zig").assert;
33
const endian = @import("endian.zig");
44

55
error SigInterrupt;
6-
error Unexpected;
76
error Io;
87
error TimedOut;
98
error ConnectionReset;

0 commit comments

Comments
 (0)