Skip to content

Make env.php swap in magento:cleanup_cache_prefix atomic - #4276

Open
sprankhub wants to merge 1 commit into
deployphp:masterfrom
sprankhub:fix-magento2-atomic-env-swap
Open

sprankhub wants to merge 1 commit into
deployphp:masterfrom
sprankhub:fix-magento2-atomic-env-swap

Conversation

@sprankhub

Copy link
Copy Markdown
Contributor

Problem

magento:cleanup_cache_prefix deletes the shared env.php and only afterwards moves env_tmp.php into its place:

run('rm {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
run('rm {{release_or_current_path}}/' . ENV_CONFIG_FILE_PATH);
run('mv {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH . ' {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
run('{{bin/symlink}} {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH);

Between the first rm and the mv there is a window in which shared/app/etc/env.php does not exist. That window matters, because the documented hook order for this feature is

after('deploy:shared', 'magento:set_cache_prefix');
after('deploy:magento', 'magento:cleanup_cache_prefix');

deploy:magento finishes before deploy:symlink, so when the task runs, the previous release is still the one serving traffic, and its app/etc/env.php symlink points at the shared file that was just deleted. Every request served in that window cannot read the configuration. The window spans two run() calls, i.e. two SSH round trips — roughly 1.4 s on our deployments, which is plenty to hit on a busy shop.

The rm of the release's own symlink has the same effect, and it is redundant regardless: {{bin/symlink}} is ln -nfs, which already replaces an existing link.

Fix

Collapse the two rms and the mv into a single mv -f. rename(2) is atomic, so a reader sees either the old env.php or the new one, never a missing file.

Notes

  • The resulting state is unchanged: shared/app/etc/env.php holds what env_tmp.php held, and the release symlink points at it.
  • As a side effect the task is now safe to re-run: if env_tmp.php is absent, mv fails and env.php is left intact, where previously it would have been deleted with nothing to replace it.
  • docs/recipe/magento2.md was regenerated with bin/docgen; the diff there is line-number references only.

The task deleted shared/app/etc/env.php before moving env_tmp.php into
place. With the documented hook order the task runs before deploy:symlink,
so the previous release is still being served and its env.php symlink
dangles for the duration of two SSH round trips.

A single mv -f is an atomic rename. The rm of the release symlink is
dropped as well, because {{bin/symlink}} is ln -nfs and already replaces
an existing link.

This branch has not been deployed

No deployments
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.

1 participant