Fix eof error for incomplete commands.

This commit fixes an issue where Tile38 will fail to start
because the AOF file contains a partially written command, which
is caused by the server not having enough disk space to complete
the previous write.

This was discovered and reported by a Theresa D on the Tile38
Slack channel.
This commit is contained in:
tidwall 2022-04-12 15:43:44 -07:00
parent 83cedde2df
commit a1cc8e6e46
1 changed files with 22 additions and 28 deletions

View File

@ -55,19 +55,19 @@ func (s *Server) loadAOF() (err error) {
var buf []byte var buf []byte
var args [][]byte var args [][]byte
var packet [0xFFFF]byte var packet [0xFFFF]byte
var zeros int
for { for {
n, err := s.aof.Read(packet[:]) n, err := s.aof.Read(packet[:])
if err != nil { if err != nil {
if err == io.EOF { if err != io.EOF {
if len(buf) > 0 { return err
return io.ErrUnexpectedEOF
} }
if zeros > 0 { if len(buf) > 0 {
// Trailing zeros in AOF. Truncate the file so it's sane. // There was an incomplete command or other data at the end of
// See issue #230 for more information. Force a warning. // the AOF file. Attempt to recover the file by truncating the
log.Infof("Truncating %d zeros from AOF (issue #230)", zeros) // file at the end position of the last complete command.
s.aofsz -= zeros log.Warnf("Truncating %d bytes due to an incomplete command\n",
len(buf))
s.aofsz -= len(buf)
if err := s.aof.Truncate(int64(s.aofsz)); err != nil { if err := s.aof.Truncate(int64(s.aofsz)); err != nil {
return err return err
} }
@ -77,8 +77,6 @@ func (s *Server) loadAOF() (err error) {
} }
return nil return nil
} }
return err
}
s.aofsz += n s.aofsz += n
data := packet[:n] data := packet[:n]
if len(buf) > 0 { if len(buf) > 0 {
@ -86,16 +84,12 @@ func (s *Server) loadAOF() (err error) {
} }
var complete bool var complete bool
for { for {
if len(data) > 0 { if len(data) > 0 && data[0] == 0 {
if data[0] == 0 { // Zeros found in AOF file (issue #230).
zeros++ // Just ignore it and move the next byte.
data = data[1:] data = data[1:]
continue continue
} }
if zeros > 0 {
return clientErrorf("Zeros found in AOF file (issue #230)")
}
}
complete, args, _, data, err = redcon.ReadNextCommand(data, args[:0]) complete, args, _, data, err = redcon.ReadNextCommand(data, args[:0])
if err != nil { if err != nil {
return err return err