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

On Linux, a file that is re-opened in readWrite mode can't be written to #337

Closed
dymk opened this issue Oct 10, 2013 · 1 comment
Closed

Comments

@dymk
Copy link

dymk commented Oct 10, 2013

Given this code:

import vibe.core.file;
import std.file;
import std.stdio;

void main() {
    string f = "testbugfile";

    scope(exit) { remove(f); }
    if(exists(f))
        remove(f);

    auto handle = openFile(f, FileMode.createTrunc);
    handle.write(cast(ubyte[])[1, 2, 3]);

    handle.close();

    handle = openFile(f, FileMode.readWrite);
    handle.write(cast(ubyte[])[5, 6, 7]);
}

The code will fail on line 18, with the error:

object.Exception@vendor/vibe-d/source/vibe/core/drivers/threadedfile.d(182): Failed to write data to disk.3 9 -1 8
----------------
./fileerr(pure @safe bool std.exception.enforce!(bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x5e) [0x5b1b2a]
./fileerr(void vibe.core.drivers.threadedfile.ThreadedFileStream.write(const(ubyte[]))+0xe6) [0x52c102]
./fileerr(_Dmain+0x141) [0x51f35d]
./fileerr(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runMain().void __lambda1()+0x18) [0x59d6ac]
./fileerr(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x59cee6]
./fileerr(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runMain()+0x27) [0x59d66b]
./fileerr(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x59cee6]
./fileerr(_d_run_main+0x1a3) [0x59ce67]
./fileerr(main+0x17) [0x51f99b]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f5e5b333ea5]

After some googling, it seems that error code 9 (the 9 in 3 9 -1 8 as found here. However, it's also listed as EBADF, according to this. I'm not sure which one the 9 here refers to.

I'm not particularly sure why this would happen; it seems that calling .close() on the handle would close the underlying fd, according to threadedfile.d. Weird.

@s-ludwig
Copy link
Member

Testing on Linux confirms that the missing O_RDWR was the cause.

BTW for me it was already the first write that failed. It never got to open the file for a second time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants