jenkinsci / lockable-resources-plugin

Lock resources against concurrent use
https://plugins.jenkins.io/lockable-resources
MIT License
87 stars 185 forks source link

Update to 1309.ve21874387edf logs NullPointerException when old build is discarded #707

Open PayBas opened 2 months ago

PayBas commented 2 months ago

Jenkins and plugins versions report

Environment ```text Jenkins: 2.462.2 OS: Linux - 6.10.10-200.fc40.x86_64 Java: 11.0.24 - Red Hat, Inc. (OpenJDK 64-Bit Server VM) --- ```

What Operating System are you using (both controller, and any agents involved in the problem)?

Fedora 40, both for controller and agents

Reproduction steps

  1. Have a pipeline job that uses a lockable resource.
  2. Set a "Discard old builds" option with "Log Rotation" strategy and "Max # of builds to keep" = 3.
  3. Run the pipeline job 4 times.
  4. Observe the Jenkins controller logs.

Expected Results

No NPE

Actual Results

Sep 20, 2024 12:50:28 AM INFO org.jenkins.plugins.lockableresources.queue.LockRunListener onDeleted
my-pipeline-job resource1
Sep 20, 2024 12:50:28 AM WARNING jenkins.util.Listeners lambda$notify$0
null
java.lang.NullPointerException
    at PluginClassLoader for lockable-resources//org.jenkins.plugins.lockableresources.LockableResourcesManager.unlockBuild(LockableResourcesManager.java:655)
    at PluginClassLoader for lockable-resources//org.jenkins.plugins.lockableresources.queue.LockRunListener.onDeleted(LockRunListener.java:110)
    at hudson.model.listeners.RunListener.lambda$fireDeleted$4(RunListener.java:271)
    at jenkins.util.Listeners.lambda$notify$0(Listeners.java:59)
    at jenkins.util.Listeners.notify(Listeners.java:67)
    at hudson.model.listeners.RunListener.fireDeleted(RunListener.java:269)
    at hudson.model.Run.delete(Run.java:1646)
    at hudson.tasks.LogRotator.perform(LogRotator.java:167)
    at hudson.model.Job.logRotate(Job.java:486)
    at jenkins.model.JobGlobalBuildDiscarderStrategy.apply(JobGlobalBuildDiscarderStrategy.java:54)
    at jenkins.model.BackgroundGlobalBuildDiscarder.lambda$processJob$0(BackgroundGlobalBuildDiscarder.java:67)
    at java.base/java.lang.Iterable.forEach(Iterable.java:75)
    at jenkins.model.BackgroundGlobalBuildDiscarder.processJob(BackgroundGlobalBuildDiscarder.java:61)
    at jenkins.model.BackgroundGlobalBuildDiscarder.execute(BackgroundGlobalBuildDiscarder.java:55)
    at hudson.model.AsyncPeriodicWork.lambda$doRun$0(AsyncPeriodicWork.java:102)
    at java.base/java.lang.Thread.run(Thread.java:829)

Anything else?

No response

Are you interested in contributing a fix?

No response

jimklimov commented 1 month ago

FWIW, this seems to be one of changes from PR #673, with new methods and lines at https://github.com/jenkinsci/lockable-resources-plugin/blob/e21874387edf2f8ab41041d73ff22e85e1cf64c4/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java#L646-L659

An NPE at line 655 probably means resourcesInUse.size() failed because resourcesInUse == null.

One chance for that could be findAndInitAction() returning null (e.g. because build is null, which could make sense during deletion but we check just above that it is not - and a null at this point would throw NPE to call the next method in chain):

...or getCurrentUsedResourceNames() returning null because that what resourcesInUse would be?.. should not happen either, it is pre-initialized and never deleted AFAIK.

Udo10744 commented 1 month ago

We have the same issue on Windows Server/Slaves, with a lightly different/detailed error message: Cannot invoke "java.util.List.size()" because "resourcesInUse" is null

2024-10-14 04:02:33.222+0000 [id=3374] INFO o.j.p.l.queue.LockRunListener#onDeleted: VCCP » A9 » Full #274 2024-10-14 04:02:33.222+0000 [id=3374] WARNING jenkins.util.Listeners#lambda$notify$0 java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "resourcesInUse" is null at PluginClassLoader for lockable-resources//org.jenkins.plugins.lockableresources.LockableResourcesManager.unlockBuild(LockableResourcesManager.java:655) at PluginClassLoader for lockable-resources//org.jenkins.plugins.lockableresources.queue.LockRunListener.onDeleted(LockRunListener.java:110) at hudson.model.listeners.RunListener.lambda$fireDeleted$4(RunListener.java:271) at jenkins.util.Listeners.lambda$notify$0(Listeners.java:59) at jenkins.util.Listeners.notify(Listeners.java:67) at hudson.model.listeners.RunListener.fireDeleted(RunListener.java:269) at hudson.model.Run.delete(Run.java:1646) at hudson.tasks.LogRotator.perform(LogRotator.java:182) at jenkins.model.SimpleGlobalBuildDiscarderStrategy.apply(SimpleGlobalBuildDiscarderStrategy.java:61) at jenkins.model.BackgroundGlobalBuildDiscarder.lambda$processJob$0(BackgroundGlobalBuildDiscarder.java:64) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at jenkins.model.BackgroundGlobalBuildDiscarder.processJob(BackgroundGlobalBuildDiscarder.java:60) at jenkins.model.BackgroundGlobalBuildDiscarder.execute(BackgroundGlobalBuildDiscarder.java:55) at hudson.model.AsyncPeriodicWork.lambda$doRun$0(AsyncPeriodicWork.java:102) at java.base/java.lang.Thread.run(Thread.java:1583)

mschwehl commented 3 weeks ago

On my Installation same error in 1322.v102a_16ce25ec

Lockable-version1322.v102a_16ce25ec Jenkins: 2.4.6.2.3 jdk21

` Oct 28, 2024 9:35:13 AM jenkins.util.Listeners lambda$notify$0 WARNING: null java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "resourcesInUse" is null at PluginClassLoader for lockable-resources//org.jenkins.plugins.lockableresources.LockableResourcesManager.unlockBuild(LockableResourcesManager.java:655) at PluginClassLoader for lockable-resources//org.jenkins.plugins.lockableresources.queue.LockRunListener.onDeleted(LockRunListener.java:110) at hudson.model.listeners.RunListener.lambda$fireDeleted$4(RunListener.java:271) at jenkins.util.Listeners.lambda$notify$0(Listeners.java:59) at jenkins.util.Listeners.notify(Listeners.java:67) at hudson.model.listeners.RunListener.fireDeleted(RunListener.java:269) at hudson.model.Run.delete(Run.java:1646) at hudson.tasks.LogRotator.perform(LogRotator.java:167) at hudson.model.Job.logRotate(Job.java:486) at PluginClassLoader for workflow-job//org.jenkinsci.plugins.workflow.job.WorkflowRun.lambda$finish$3(WorkflowRun.java:665) at jenkins.security.ImpersonatingScheduledExecutorService$1.run(ImpersonatingScheduledExecutorService.java:67) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.base/java.util.concurrent.FutureTask.run(Unknown Source) at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.base/java.lang.Thread.run(Unknown Source)

`