Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli:apply: fix potential race with rename/creation of netdevs and start networkd if off (LP: #1962095) #260

Merged
merged 5 commits into from
Mar 10, 2022

Conversation

slyon
Copy link
Collaborator

@slyon slyon commented Feb 7, 2022

Description

Calling networkctl_reload() before networkd_interfaces() makes sure that newly created netdevs will show up in the interface list.

Making use of the index number (instead of interface name) ensures that renamed interfaces are properly reconfigured even if they changed their name.

Starting networkd if not active is making sure we can process network changes even if systemd-networkd was stopped before (e.g. by subiquity), see LP#1962095

Checklist

  • Runs make check successfully.
  • Retains 100% code coverage (make check-coverage).
  • New/changed keys in YAML format are documented.
  • (Optional) Adds example YAML for new feature.
  • (Optional) Closes an open bug in Launchpad: LP#1962095

@slyon slyon force-pushed the slyon/fix-netplan-apply-race branch from 2952644 to c14ad8a Compare March 4, 2022 13:38
@codecov-commenter
Copy link

codecov-commenter commented Mar 4, 2022

Codecov Report

Merging #260 (d0e4d3e) into main (20182f9) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #260   +/-   ##
=======================================
  Coverage   99.07%   99.07%           
=======================================
  Files          61       61           
  Lines       10860    10867    +7     
=======================================
+ Hits        10760    10767    +7     
  Misses        100      100           
Impacted Files Coverage Δ
netplan/cli/commands/apply.py 100.00% <ø> (ø)
netplan/cli/utils.py 100.00% <100.00%> (ø)
tests/test_utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 20182f9...d0e4d3e. Read the comment docs.

@slyon slyon changed the title cli:apply: fix potential race with rename/creation of netdevs cli:apply: fix potential race with rename/creation of netdevs and start networkd if off (LP: #1962095) Mar 7, 2022
@slyon slyon marked this pull request as ready for review March 7, 2022 15:04
@slyon
Copy link
Collaborator Author

slyon commented Mar 7, 2022

This is related to canonical/core-base#31

@slyon slyon requested a review from sil2100 March 7, 2022 15:58
@slyon slyon force-pushed the slyon/fix-netplan-apply-race branch 3 times, most recently from a58fbc2 to 5286cd3 Compare March 9, 2022 10:58
Calling networkctl_reload() before networkd_interfaces() makes sure that newly created netdevs will show up in the interface list.

Making use of the index number (instead of interface name) ensures that renamed interfaces are properly reconfigured even if they changed their name.
… time

When a MAC address is set for a bond, networkd will set the same MAC address to
the enslaved interfaces. Therefore we cannot match on the original MAC for the
ethernet device, as networkd will not find/manage that interface otherwise.

https://systemd.network/systemd.netdev.html#MACAddress=
@slyon slyon force-pushed the slyon/fix-netplan-apply-race branch from 5286cd3 to d0e4d3e Compare March 9, 2022 11:38
@slyon
Copy link
Collaborator Author

slyon commented Mar 9, 2022

@valentindavid just confirmed via PM that this PR fixes the issue they see on a real system. Tested via PPA https://launchpad.net/~slyon/+archive/ubuntu/netplan/+sourcepub/13303987/+listing-archive-extra

except subprocess.CalledProcessError:
# (re-)start systemd-networkd if it is not running, yet
logging.warning('Falling back to a hard restart of systemd-networkd.service')
utils.systemctl('restart', ['systemd-networkd.service'], sync=True)
Copy link
Contributor

@xnox xnox Mar 9, 2022

Choose a reason for hiding this comment

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

Generally, I don't like try/except blocks, and especially when they wrap two commands without distinction if networkd is not running (hence networctl fails to connect to networkd) or if there is something broken with the config (and thus reconfigure of a device fails, but networkd is running and gets pointlessly restarted).

Can you check if systemd-networkd.service is running? If yes, execute networkctl reload & reconfigure. Otherwise restart networkd.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you check if systemd-networkd.service is running? If yes, execute networkctl reload & reconfigure. Otherwise restart networkd.

That is actually how I had it implemented initially, but it does not catch cases where systemd-networkd.service is running but "stuck" (for some unkown reason), as reported in the original bug report at LP#1962095 (comment 4). Sure, networkd being stuck is not really a netplan problem... but using the try/except block we can hit two birds with one stone.

Copy link
Collaborator

@sil2100 sil2100 left a comment

Choose a reason for hiding this comment

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

This looks good to me, and feels sufficiently safe. Sure, as per Dimitri's comment it might lead to a case where there's a pointless restart, but I don't think that will be too frequent, and other ways might just unnecessarily make the logic more complicated. So I'd be +1 on getting this merged as-is. But I leave it up to you!

@slyon
Copy link
Collaborator Author

slyon commented Mar 10, 2022

Thank you for the comments. As per my answer above, I'll merge this as-is.

@slyon slyon merged commit a4b70e7 into main Mar 10, 2022
@slyon slyon deleted the slyon/fix-netplan-apply-race branch March 10, 2022 08:41
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.

4 participants