Skip to content

Conversation

@Farjaad
Copy link

@Farjaad Farjaad commented Jan 28, 2026

Overview

This PR introduces an :advisory_lock_timeout_seconds configuration option that triggers an error when an advisory lock cannot be obtained within the specified time limit.

Motivation

We experienced jobs that would wait for several minutes executing SELECT GET_LOCK until Sidekiq terminated them. This created queue blockages and led to significant job backlogs. Implementing a timeout enables jobs to fail quickly and retry during periods of lower contention, ultimately completing successfully.

Example Usage

class Tag < ApplicationRecord
  has_closure_tree advisory_lock_timeout_seconds: 30
end

Behavior

When advisory_lock_timeout_seconds is configured, a WithAdvisoryLock::FailedToAcquireLock exception is raised if the lock cannot be secured within the designated timeframe, creating explicit failure handling for lock contention scenarios.

Without this configuration, the system maintains the default behavior of waiting indefinitely until lock acquisition.

Implementation Details

  • Introduces advisory_lock_timeout_seconds configuration option
  • Invokes with_advisory_lock! with lock parameters when advisory_lock_timeout_seconds is specified

Backward Compatibility

The advisory_lock_timeout_seconds option defaults to nil, and with_advisory_lock! is only invoked when a non-null value is supplied.

- Add advisory_lock_timeout_seconds option (default: 15 seconds)
- Raise error when advisory lock cannot be acquired within timeout
- This provides explicit failure behavior for lock contention issues

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@Farjaad Farjaad force-pushed the advisory-lock-timeout-seconds branch 6 times, most recently from 0eb42a6 to 56a2ea7 Compare January 28, 2026 21:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an advisory_lock_timeout_seconds configuration option that allows closure_tree to raise an error when an advisory lock cannot be acquired within a specified timeout period, preventing indefinite blocking and enabling jobs to fail fast and retry during lower contention.

Changes:

  • Added advisory_lock_timeout_seconds configuration option with validation
  • Implemented logic to call with_advisory_lock! instead of with_advisory_lock when timeout is configured
  • Added tests to verify the new timeout functionality
  • Updated documentation to describe the new option

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
lib/closure_tree/support.rb Adds default option, validation logic, and conditional method selection based on timeout configuration
lib/closure_tree/support_attributes.rb Implements advisory_lock_options helper method to build options hash with timeout
lib/closure_tree/has_closure_tree.rb Adds advisory_lock_timeout_seconds to the list of valid configuration keys
test/closure_tree/support_test.rb Adds unit tests for initialization and method selection based on timeout configuration
test/closure_tree/parallel_test.rb Updates stub signature to accept options parameter for backward compatibility
README.md Documents the new advisory_lock_timeout_seconds configuration option

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Farjaad Farjaad force-pushed the advisory-lock-timeout-seconds branch from cfe33f5 to 2456d46 Compare January 31, 2026 17:19
@Farjaad Farjaad force-pushed the advisory-lock-timeout-seconds branch from 2456d46 to 91c90af Compare January 31, 2026 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants