discourse/config/initializers/005-site_settings.rb
Ted Johansson 09d6010c7b
DEV: Detect circular site setting dependencies (#38165)
### Background

In #36061, dependent site settings were introduced through the
`depends_on` key. This is used internally to construct a dependency
graph and sort settings in order-of-dependency before bulk updates.

However, no safeguards were introduced to avoid circular dependencies.
This means a `TSort::Cyclic` could come along and ruin the day at
arbitrary points during runtime.

### What is this change?

This change does two things:

1. Rescue, format, and re-raise `TSort::Cyclic` exceptions.
2. Eagerly order settings on app reload to catch cyclic dependencies
up-front.
2026-03-04 10:10:38 +10:00

33 lines
953 B
Ruby

# frozen_string_literal: true
# load up git version into memory
# this way if it changes underneath we still have
# the original version
Discourse.git_version
if GlobalSetting.skip_redis?
require "site_settings/local_process_provider"
Rails.cache = Discourse.cache
Rails.application.config.to_prepare do
SiteSetting.provider = SiteSettings::LocalProcessProvider.new
end
return
end
Rails.application.config.to_prepare do
RailsMultisite::ConnectionManagement.safe_each_connection do
begin
SiteSetting.refresh!
unless String === SiteSetting.push_api_secret_key &&
SiteSetting.push_api_secret_key.length == 32
SiteSetting.push_api_secret_key = SecureRandom.hex
end
# Check for circular dependencies in site settings.
SiteSetting.type_supervisor.dependencies.order
rescue ActiveRecord::StatementInvalid
# This will happen when migrating a new database
end
end
end