package_control icon indicating copy to clipboard operation
package_control copied to clipboard

auto_upgrade_frequency as a string raises TypeError in the startup thread; resume_indexer() and error report are skipped

Open dpc00 opened this issue 2 months ago • 2 comments

A non-numeric auto_upgrade_frequency (e.g. "24") raises TypeError in the startup thread, which skips resume_indexer() and the clean-up error report.

AutomaticUpgrader.load_last_run() does self.next_run = int(self.last_run) + (frequency * 60 * 60) on whatever the setting holds. A number in quotes is an easy mistake in a hand-edited settings file (load_list_setting exists precisely because users make that kind of mistake for list settings), and "24" * 60 * 60 is a 7200-character string.

Verified in Sublime Text 4 (Package Control 4.2.8; automatic_upgrader.py is identical in master apart from imports) by calling load_last_run() on the real class with a stub manager (reading the real last_run.json, writing nothing):

24        -> OK
'24'      -> TypeError: unsupported operand type(s) for +: 'int' and 'str'
'abc'     -> TypeError
[24]      -> TypeError: unsupported operand type(s) for +: 'int' and 'list'
None, '', 0 -> OK (next_run 0, i.e. upgrade check every start)

Why it matters beyond the single feature: PackageCleanup.run() (the startup thread) is a straight sequence with no try/finally:

if self.manager.settings.get('auto_upgrade'):
    AutomaticUpgrader(self.manager).run()

# make sure to restore indexing state
# note: required after Package Control upgrade
self.resume_indexer()

if self.failed_cleanup:
    show_error(...)

so any exception out of AutomaticUpgrader.run() means resume_indexer() and the "Package clean-up could not be completed" dialog are skipped for that startup, and for every following startup as long as the setting stays wrong. resume_indexer() is what puts index_files back to true in Preferences after PackageDisabler set it to false for an operation (it reads backup.json, which survives a restart, e.g. after a Package Control upgrade). I did not reproduce the stuck-index_files end state (that would require running a real upgrade cycle); it follows from reading those code paths, so please treat that last consequence as unconfirmed. The TypeError itself is confirmed.

Related, by reading only: load_last_run catches (FileNotFoundError, ValueError, TypeError) but not KeyError, so a last_run.json that parses but lacks timestamp/st_version would also escape.

Fix: coerce/validate the setting (float(frequency) in a try, falling back to a default) and add KeyError to the except tuple; wrap the AutomaticUpgrader call in PackageCleanup.run() in try/except Exception + console_write, or move resume_indexer() into a finally.

dpc00 avatar Sep 24 '26 17:09 dpc00

Context for the unconfirmed part: the end state I couldn't reproduce here, index_files left at false in Preferences, has been hit by real users before via a different route, #1639 (closed, fixed in 4.0.0-beta2: backup_and_reset_settings() stored the temporary False instead of the user's original value). That report shows it goes unnoticed for a while and silently disables indexing, which is why skipping resume_indexer() in the startup thread deserves a finally. This issue is just another way to skip it; the TypeError above is the confirmed part.

dpc00 avatar Sep 24 '26 17:09 dpc00

A second trigger for the same unguarded call: AutomaticUpgrader.upgrade_packages() passes ignore_packages=settings.get('auto_upgrade_ignore') straight into PackageTaskRunner.upgrade_packages, which validates its type and raises for anything that isn't None, a string, a list, a tuple or a set. The default and documented value is a list (// Packages to not auto upgrade, []), but e.g. true/false (someone reading it as an on/off switch) or an object are easy to write.

Verified in Sublime Text 4 (4.2.8) by calling PackageTaskRunner().upgrade_packages(ignore_packages=v, unattended=True); the type check runs before any task is created, so nothing was upgraded:

[] / None / 'MyPkg' / ('a',) / {'a'}  -> accepted
True / False / {'MyPkg': True} / 5 / 0 -> TypeError: Argument 'ignore_packages' must be a string, list or set!

That TypeError goes up through AutomaticUpgrader.run() into PackageCleanup.run() in the same way as the auto_upgrade_frequency case, so the same fix (guard/finally around the upgrader call in the startup thread, so resume_indexer() and the clean-up error dialog still run, and log the problem instead of crashing) covers both.

dpc00 avatar Sep 24 '26 17:09 dpc00