input and output buffers get out of sync in io streams

Bug #1600610 reported by Guillaume LE VAILLANT on 2016-07-10
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
High
Unassigned

Bug Description

(with-open-file (stream "/tmp/foo"
                        :direction :io
                        :if-exists :overwrite
                        :if-does-not-exist :create)
  (write-string "abc" stream)
  (file-position stream 0)
  (write-char #\x stream)
  (read-char stream))
=> #\a

Stas Boukarev (stassats) on 2016-07-11
summary: - read-sequence can move file-position beyond end of file
+ input and output buffers get out of sync in io streams
description: updated
Changed in sbcl:
status: New → Triaged
importance: Undecided → High
tags: added: streams

Flushing the output buffer by hand before reading seems to work.

(with-open-file (stream "string.txt"
                        :direction :io
                        :if-exists :overwrite
                        :if-does-not-exist :create)
  (write-string "abc" stream)
  (file-position stream 0)
  (write-char #\d stream)
  (finish-output stream)
  (read-char stream))
=> #\b

Maybe the code for flushing the output buffer before reading in the case of a file stream opened in io mode is missing...

The attached patch seems to fix the issue. It adds calls to finish-fd-stream-output in input-wrapper and input-wrapper/variable-width. However, I'm not familiar with the internals of SBCL, so it might not be the best place to put them...

Stas Boukarev (stassats) wrote :

Flushing on each read will kill performance.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers