Locking doesn't work across multiple nodes?

28 views
Skip to first unread message

Jonathan del Strother

unread,
Apr 7, 2017, 11:24:32 AM4/7/17
to Jenkins Users
Are pipeline locks expected to behave when used by two nodes simultaneously?

My Jenkinsfile defines the following helper - 

def log(msg) {
  echo((new Date().format("yyyy-MM-dd'T'HH:mm:ss: ")) + msg)
}

def lockDB(number, callback) {
  try {
    log "${number}: Acquiring lock ..."
    lock(resource: "boom-test-database-${number}", inversePrecedence: true) {
      log "${number}: Acquired!"
      callback()
      log "${number}: Releasing..."
    }
  } finally {
    log "${number}: Released!"
  }
}


I somewhat frequently see two nodes manage to acquire the same lock simultaneously, as in the following example, where master locked a resource from 14:45:57 -> 14:52:04, and the slave locked the same resource from 14:51:24 -> 14:56:43.


[featureTests] Running on master in /var/jenkins_home/workspace/BoomMultiPipeline_master-YRHUUT2RENAD5FNHS4YJ5GQKTJDN2M7B3KGAU3ELBWX7MZ2AEKBA
[featureTests] 2017-04-07T14:40:44: 1: Acquiring lock ...
[Pipeline] [featureTests] lock
[featureTests] Trying to acquire lock on [boom-test-database-1]
[featureTests] Found 0 available resource(s). Waiting for correct amount: 1.
[featureTests] [boom-test-database-1] is locked, waiting...
[featureTests] Lock acquired on [boom-test-database-1]
[featureTests] 2017-04-07T14:45:57: 1: Acquired!
... running tests
[featureTests] Lock released on resource [boom-test-database-1]
[featureTests] 2017-04-07T14:52:04: 1: Released!




[featureTests] Running on slave in /var/jenkins_home/workspace/eline_tb_remaining_indices2-DTE7IMSYRVCF6GO3T5IRRR52R4YQ3GSRFNEEKCRH7FGSUBQB4SFQ
[featureTests] 2017-04-07T14:49:33: 1: Acquiring lock ...
[Pipeline] [featureTests] lock
[featureTests] Trying to acquire lock on [boom-test-database-1]
[featureTests] Found 0 available resource(s). Waiting for correct amount: 1.
[featureTests] [boom-test-database-1] is locked, waiting...
[featureTests] Lock acquired on [boom-test-database-1]
[featureTests] 2017-04-07T14:51:24: 1: Acquired!
... running tests
[featureTests] Lock released on resource [boom-test-database-1]
[featureTests] 2017-04-07T14:56:43: 1: Released!



Is this a bug, or am I misunderstanding how locking is supposed to work in Jenkins?

Thomas Rambrant

unread,
Jun 14, 2017, 11:18:53 AM6/14/17
to Jenkins Users
I have the same problem so I'm very interested in a solution... 

I have tried everything I can think of. I'm currently locking on the outmost level, so that every stage and node blocks is inside the lock. I previously had it inside a node block  but got the impression that the lock only worked on the same node. But setting it on the outmost level still fails sometimes, even for jobs execited on the same node...

So I think this is a bug somehow...

/Thomas
Reply all
Reply to author
Forward
0 new messages