GithubHelp home page GithubHelp logo

filequeue's People

Contributors

daddz avatar hyandell avatar jbodah avatar max-mapper avatar pezra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

filequeue's Issues

Error if empty? called immediately

The length method doesn't protect from a file that doesn't exist yet:


$ irb
irb(main):001:0> require 'filequeue'
=> true
irb(main):002:0> queue = FileQueue.new 'bob.txt'
=> #<FileQueue:0x007fb2cc2c10b0 @delimiter="\n", @file_name="bob.txt">
irb(main):003:0> queue.empty?
Errno::ENOENT: No such file or directory - bob.txt
from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:51:in initialize' from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:51:inopen'
from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:51:in safe_open' from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:35:inlength'
from /Library/Ruby/Gems/2.0.0/gems/filequeue-0.0.2/lib/filequeue.rb:42:in empty?' from (irb):3 from /usr/bin/irb:12:in

'
irb(main):004:0> quit

Race condition on pop

I may be wrong but it seems like the following code has a potential race condition:

    safe_open 'r' do |file|
      value = file.gets @delimiter
      rest = file.read
    end
    safe_open 'w+' do |file|
      file.write rest
    end

e.g:

A obtains lock; A reads line; A caches rest
B obtains lock; B reads line; B caches rest
B obtains lock; B writes rest
A obtains lock; A writes rest

Fixed by not releasing the lock until rest is written back to the file:

FileQueue.class_eval do
  def pop
    value = nil
    safe_open 'r+' do |file|
      value = file.gets @delimiter
      rest = file.read
      file.rewind
      file.truncate(0)
      file.write(rest)
    end
    value ? value[0..-(@delimiter.length) - 1] : nil
  end
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.