Skip to content

Commit 8574e24

Browse files
JeffBezansonstaticfloat
authored andcommitted
propagate errors from flush and close within ios_close (#35326)
1 parent 5bd1d39 commit 8574e24

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ Standard library changes
116116
order from `order` is used to compare the values of `by(element)`. In the latter case,
117117
any order different from `Forward` or `Reverse` will raise an error about the
118118
ambiguity.
119+
* `close` on a file (`IOStream`) can now throw an exception if an error occurs when trying
120+
to flush buffered data to disk ([#35303]).
119121

120122
#### LinearAlgebra
121123
* The BLAS submodule now supports the level-2 BLAS subroutine `hpmv!` ([#34211]).

base/iostream.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ to synchronous `File`'s and `IOStream`'s not to any of the asynchronous streams.
4242
fd(s::IOStream) = Int(ccall(:jl_ios_fd, Clong, (Ptr{Cvoid},), s.ios))
4343

4444
stat(s::IOStream) = stat(fd(s))
45-
close(s::IOStream) = @lock_nofail s.lock ccall(:ios_close, Cvoid, (Ptr{Cvoid},), s.ios)
45+
4646
isopen(s::IOStream) = ccall(:ios_isopen, Cint, (Ptr{Cvoid},), s.ios) != 0
47+
48+
function close(s::IOStream)
49+
bad = @lock_nofail s.lock ccall(:ios_close, Cint, (Ptr{Cvoid},), s.ios) != 0
50+
systemerror("close", bad)
51+
end
52+
4753
function flush(s::IOStream)
4854
sigatomic_begin()
4955
bad = @lock_nofail s.lock ccall(:ios_flush, Cint, (Ptr{Cvoid},), s.ios) != 0
5056
sigatomic_end()
5157
systemerror("flush", bad)
5258
end
59+
5360
iswritable(s::IOStream) = ccall(:ios_get_writable, Cint, (Ptr{Cvoid},), s.ios)!=0
61+
5462
isreadable(s::IOStream) = ccall(:ios_get_readable, Cint, (Ptr{Cvoid},), s.ios)!=0
5563

5664
"""

src/support/ios.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -648,17 +648,21 @@ int ios_flush(ios_t *s)
648648
return 0;
649649
}
650650

651-
void ios_close(ios_t *s)
651+
int ios_close(ios_t *s)
652652
{
653-
ios_flush(s);
654-
if (s->fd != -1 && s->ownfd)
655-
close(s->fd);
653+
int err = ios_flush(s);
654+
if (s->fd != -1 && s->ownfd) {
655+
int err2 = close(s->fd);
656+
if (err2 != 0)
657+
err = err2;
658+
}
656659
s->fd = -1;
657660
if (s->buf!=NULL && s->ownbuf && s->buf!=&s->local[0]) {
658661
LLT_FREE(s->buf);
659662
}
660663
s->buf = NULL;
661664
s->size = s->maxsize = s->bpos = 0;
665+
return err;
662666
}
663667

664668
int ios_isopen(ios_t *s)

src/support/ios.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ JL_DLLEXPORT int ios_trunc(ios_t *s, size_t size) JL_NOTSAFEPOINT;
9393
JL_DLLEXPORT int ios_eof(ios_t *s);
9494
JL_DLLEXPORT int ios_eof_blocking(ios_t *s);
9595
JL_DLLEXPORT int ios_flush(ios_t *s);
96-
JL_DLLEXPORT void ios_close(ios_t *s);
96+
JL_DLLEXPORT int ios_close(ios_t *s);
9797
JL_DLLEXPORT int ios_isopen(ios_t *s);
9898
JL_DLLEXPORT char *ios_take_buffer(ios_t *s, size_t *psize); // release buffer to caller
9999
// set buffer space to use

0 commit comments

Comments
 (0)