graveyard / gearman-node

node bindings to the gearman tcp protocol

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

race condition

rgarcia opened this issue · comments

gearman-coffee/lib/client.coffee:126
        _this.jobs[handle].emit('fail', handle);
                           ^
TypeError: Cannot call method 'emit' of undefined

Here's a recreation of this issue:

EventEmitter = require("events").EventEmitter

class Connection extends EventEmitter
  sendCommand: (cmd, name) =>
    if cmd is 'submit'
      setTimeout () =>
        @emit 'created', name
      , 1000
      setTimeout () =>
        console.log 'emitting fail', name
        @emit 'fail', name
      , 2000

class Client extends Connection
  constructor: () ->
    @jobs={}
    @on 'fail', (handle) ->
      console.log 'fail', handle, @jobs[handle]
      delete @jobs[handle]

  submitJob: (name, payload) ->
    @once 'created', (handle) ->
      console.log 'created', handle, name
      @jobs[handle] = name
    console.log 'submitting', name
    @sendCommand 'submit', name

# submit two jobs back to back
client = new Client()
client.submitJob 'test1', {}
client.submitJob 'test2', {}

output:

submitting test1
submitting test2
created test1 test1
created test1 test2
emitting fail test1
fail test1 test2
emitting fail test2
fail test2 undefined

on the second submit, calling .once is ignored. So when the second job is actually created, it overwrites the handle assigned to the first job ("created test1 test2"), and screws up the job handle mapping.