janestreet / async_unix

Jane Street Capital's asynchronous execution library (unix)
MIT License
33 stars 21 forks source link

Writer.save fails on empty contents #5

Closed agarwal closed 7 years ago

agarwal commented 9 years ago

I tried creating an empty file using Writer.save, but it fails. I can work around it by using any of several other methods, but curious why save doesn't work with empty contents. Is it intentional or is this a bug?

utop # Writer.save "foo.txt" ~contents:"";;
Exception:                                                                                                                              (src/monitor.ml.Error_
 ((exn                                                                                                                                  
   ("writer error"
    ((src/monitor.ml.Error_
      ((exn (Unix.Unix_error "Invalid argument" writev "")) (backtrace (""))
       (monitor
        (((name (id 118)) (here ()) (id 118) (has_seen_error true)
          (is_detached true) (kill_index 0))
         ((name block_on_async) (here ()) (id 115) (has_seen_error false)
          (is_detached true) (kill_index 0))))))
     ((id 9)
      (fd
       ((file_descr 46) (info foo.txtPlkeyr) (kind File)
        (supports_nonblock false) (have_set_nonblock false)
        (state (Open Empty))
        (watching ((read Not_watching) (write Not_watching)))
        (watching_has_changed false) (num_active_syscalls 1)
        (close_finished Empty)))
      (monitor
       (((name (id 117)) (here ()) (id 117) (has_seen_error true)
         (is_detached false) (kill_index 0))
        ((name block_on_async) (here ()) (id 115) (has_seen_error false)
         (is_detached true) (kill_index 0))))
      (inner_monitor
       (((name (id 118)) (here ()) (id 118) (has_seen_error true)
         (is_detached true) (kill_index 0))
        ((name block_on_async) (here ()) (id 115) (has_seen_error false)
         (is_detached true) (kill_index 0))))
      (background_writer_state Stopped_permanently) (syscall Per_cycle)
      (bytes_received 0) (bytes_written 0) (scheduled <opaque>)
      (scheduled_bytes 0) (buf <opaque>) (scheduled_back 0) (back 0)
      (flushes <opaque>) (close_state Open) (close_finished Empty)
      (close_started Empty) (producers_to_flush_at_close ())
      (flush_at_shutdown_elt (<opaque>)) (check_buffer_age ())
      (consumer_left Empty) (raise_when_consumer_leaves true)
      (open_flags (Full (Ok (rdwr))))))))
  (monitor
   (((name (id 117)) (here ()) (id 117) (has_seen_error true)
     (is_detached false) (kill_index 0))
    ((name block_on_async) (here ()) (id 115) (has_seen_error false)
     (is_detached true) (kill_index 0)))))).
ghost commented 9 years ago

That definitely looks like a bug, we'll look into it.

solongordon commented 8 years ago

I'm seeing a very similar error if I write an empty string to stdout or stderr.

open Async.Std

let () =
  print_string "";
  Core.Std.never_returns (Scheduler.go ())
((exn
  ("writer error"
   ((src/monitor.ml.Error_
     ((exn (Unix.Unix_error "Invalid argument" writev ""))
      (backtrace
       ("Raised by primitive operation at file \"src/writer0.ml\", line 410, characters 38-55"
        "Called from file \"src/job_queue.ml\", line 164, characters 6-47"
        ""))
...

It looks like this happens because Bigstring.writev blows up when called with empty input. Not sure if that case should be guarded here or in Bigstring itself.

ghost commented 8 years ago

I can't manage to reproduce any of these errors, what version of async_unix are you using?

solongordon commented 8 years ago

I'm using 113.00.00 on OS X. (I tried to test out 113.24.00 but was hitting some compilation issues.)

ghost commented 8 years ago

OK, I can reproduce this as well on OSX. I initially read the error wrong, I didn't notice it was the system call that returned EINVAL directly. Reading the OSX man page for writev it's indeed the case that you can't call writev with 0 iovectors. I'll try to get a fix for next release

solongordon commented 8 years ago

Great! Happy to submit a PR if that would be helpful.

bmillwood commented 7 years ago

I understand this is fixed in more recent versions.