nsqio / go-diskqueue

A Go package providing a filesystem-backed FIFO queue
MIT License
471 stars 101 forks source link

Close() may cause data lose #44

Open trash-boy opened 10 months ago

trash-boy commented 10 months ago
func (d *diskQueue) exit(deleted bool) error {
    d.Lock()
    defer d.Unlock()

    d.exitFlag = 1

    if deleted {
        d.logf(INFO, "DISKQUEUE(%s): deleting", d.name)
    } else {
        d.logf(INFO, "DISKQUEUE(%s): closing", d.name)
    }

    close(d.exitChan)
    // ensure that ioLoop has exited
    <-d.exitSyncChan

    close(d.depthChan)

    if d.readFile != nil {
        d.readFile.Close()
        d.readFile = nil
    }

    if d.writeFile != nil {
        d.writeFile.Close()
        d.writeFile = nil
    }

    return nil
}

d.readFile.Close() d.readFile = nil

but sync() do not d.writeFile.Sync(),may cause data loss, why not add .Sync() in exit(). for example:

func (d *diskQueue) exit(deleted bool) error {
    d.Lock()
    defer d.Unlock()

    d.exitFlag = 1

    if deleted {
        d.logf(INFO, "DISKQUEUE(%s): deleting", d.name)
    } else {
        d.logf(INFO, "DISKQUEUE(%s): closing", d.name)
    }

    close(d.exitChan)
    // ensure that ioLoop has exited
    <-d.exitSyncChan

    close(d.depthChan)

    if d.readFile != nil {
        d.readFile.Close()
        d.readFile = nil
    }

    if d.writeFile != nil {
        d.writeFile.Close()
                 d.writeFile.Sync()
        d.writeFile = nil
    }

    return nil
}
NoFacePeace commented 10 months ago

感谢您的来信,我尽快回复您!