Skip to content

Commit 0cbfea5

Browse files
committed
Add validation to snooze() and memory cleanup to isSnoozed()
1 parent 4ad3789 commit 0cbfea5

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

core/src/main/java/hudson/model/AdministrativeMonitor.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.io.IOException;
3434
import java.util.Map;
3535
import java.util.Set;
36+
import java.util.logging.Level;
37+
import java.util.logging.Logger;
3638
import jenkins.model.Jenkins;
3739
import org.kohsuke.accmod.Restricted;
3840
import org.kohsuke.accmod.restrictions.NoExternalUse;
@@ -89,6 +91,8 @@
8991
* @see Jenkins#administrativeMonitors
9092
*/
9193
public abstract class AdministrativeMonitor extends AbstractModelObject implements ExtensionPoint, StaplerProxy {
94+
private static final Logger LOGGER = Logger.getLogger(AdministrativeMonitor.class.getName());
95+
9296
/**
9397
* Human-readable ID of this monitor, which needs to be unique within the system.
9498
*
@@ -161,13 +165,34 @@ public boolean isSnoozed() {
161165
if (expiry == null) {
162166
return false;
163167
}
164-
return System.currentTimeMillis() < expiry;
168+
long now = System.currentTimeMillis();
169+
if (now >= expiry) {
170+
// Cleanup expired entry to prevent memory leak
171+
try {
172+
AbstractCIBase jenkins = Jenkins.get();
173+
Map<String, Long> map = jenkins.getSnoozedAdministrativeMonitors();
174+
if (map.remove(id) != null) {
175+
jenkins.setSnoozedAdministrativeMonitors(map);
176+
jenkins.save();
177+
}
178+
} catch (IOException e) {
179+
LOGGER.log(Level.WARNING, "Failed to cleanup expired snooze for " + id, e);
180+
}
181+
return false;
182+
}
183+
return true;
165184
}
166185

167186
/**
168187
* @since 2.549
169188
*/
170189
public void snooze(long durationMs) throws IOException {
190+
if (durationMs <= 0) {
191+
throw new IllegalArgumentException("Duration must be positive");
192+
}
193+
if (durationMs > 365L * 24 * 60 * 60 * 1000) {
194+
throw new IllegalArgumentException("Duration exceeds maximum (1 year)");
195+
}
171196
long expiryTime = System.currentTimeMillis() + durationMs;
172197
AbstractCIBase jenkins = Jenkins.get();
173198
Map<String, Long> map = jenkins.getSnoozedAdministrativeMonitors();

0 commit comments

Comments
 (0)