Skip to content

[action] [PR:4251] Add filesystem sync after plugin installation#4338

Merged
mssonicbld merged 1 commit intosonic-net:202511from
mssonicbld:cherry/202511/4251
Mar 9, 2026
Merged

[action] [PR:4251] Add filesystem sync after plugin installation#4338
mssonicbld merged 1 commit intosonic-net:202511from
mssonicbld:cherry/202511/4251

Conversation

@mssonicbld
Copy link
Collaborator

Why I did it

In some scenarios, after install plugin then power cycle, file content might lost.
Before power cycle, file size is 205, also can found register function in python file, but after power cycle, this file size is 0, so assume this is caused by page cache didn't write back to disk on time, when power cycle happen.
Before power cycle:

2026 Feb  3 10:34:16.156531 sonic-testbed INFO  [DIAGNOSTIC] Starting CLI plugins installation for package: cpu-report
2026 Feb  3 10:34:16.157013 sonic-testbed INFO  [DIAGNOSTIC] Installing CLI plugin: package=cpu-report, command=show, src=/show.py, dst=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.157177 sonic-testbed INFO  [DIAGNOSTIC] Starting extract: image=sha256:1230c222517c88863253c94dba34a788b580604618373fff24ab737a7d519c3f, src=/show.py, dst=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.267834 sonic-testbed INFO  [DIAGNOSTIC] Tar buffer size: 2048 bytes, MD5: b0b48780efda61d230dc2e3592cc3ba6
2026 Feb  3 10:34:16.268709 sonic-testbed INFO  [DIAGNOSTIC] Tar member: name=show.py, size=205, isfile=True
2026 Feb  3 10:34:16.269652 sonic-testbed INFO  [DIAGNOSTIC] File extracted successfully: path=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py, size=205, MD5=f2f3ca5258fd0685adf2cc44567934fb, elapsed=0.112s
2026 Feb  3 10:34:16.270313 sonic-testbed INFO  [DIAGNOSTIC] Python syntax validation: PASS for /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.270820 sonic-testbed INFO  [DIAGNOSTIC] Plugin file verification after extract: path=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py, size=205, MD5=f2f3ca5258fd0685adf2cc44567934fb, mtime=1684332898.0, extract_time=0.113s
2026 Feb  3 10:34:16.271351 sonic-testbed INFO  [DIAGNOSTIC] Python syntax check: PASS for /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.271638 sonic-testbed INFO  [DIAGNOSTIC] Found "def register" in plugin file: /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.271918 sonic-testbed INFO  [DIAGNOSTIC] Completed CLI plugins installation for package: cpu-report, elapsed=0.115s

After power cycle:

admin@sonic-testbed:~$ show version 2>&1
failed to import plugin show.plugins.cpu-report: module 'show.plugins.cpu-report' has no attribute 'register'

# file size is 0
admin@sonic-testbed:~$ ls -lih /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
830572 -rw-r--r-- 1 root root 0 May 17  2023 /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
# md5sum is different with previous
admin@sonic-testbed:~$ sudo md5sum /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
d41d8cd98f00b204e9800998ecf8427e  /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
# file is empty
admin@sonic-testbed:~$ sudo stat /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
  File: /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 0,27    Inode: 830572      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2026-02-03 10:34:16.266593882 +0200
Modify: 2023-05-17 17:14:58.000000000 +0300
Change: 2026-02-03 10:34:16.262593831 +0200
 Birth: 2026-02-03 10:34:16.262593831 +0200
admin@sonic-testbed:~$ cat /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
admin@sonic-testbed:~$

What I did

Fix intermittent plugin corruption after power cycle by adding os.sync() to flush filesystem buffers after all CLI plugins are installed. This prevents incomplete plugin files that cause 'module has no attribute 'register'' errors in show commands after system reboot.

How I did it

Added os.sync() system call in PackageManager._install_cli_plugins() method after all CLI plugin files are extracted and installed. This ensures that:

  1. All plugin file data is flushed from the OS page cache to disk
  2. File metadata and data are both persisted before the method returns
  3. Plugin files remain intact even if an abrupt power loss occurs shortly after installation

How to verify it

  1. Install cpu-report package: sonic-package-manager install cpu-report==1.0.0 -y
  2. Enable feature: config feature state cpu-report enabled
  3. Upgrade package: sonic-package-manager install cpu-report==1.0.7 -y
  4. Upgrade again: sonic-package-manager install cpu-report==1.0.8 -y
  5. Immediately perform power cycle
  6. After reboot, run: show version
    If there is problem, error is: failed to import plugin show.plugins.cpu-report: module 'show.plugins.cpu-report' has no attribute 'register'.

Signed-off-by: Sonic Build Admin [email protected]

<!--
    Please make sure you've read and understood our contributing guidelines:
    https://github.com/Azure/SONiC/blob/gh-pages/CONTRIBUTING.md

    ** Make sure all your commits include a signature generated with `git commit -s` **

    If this is a bug fix, make sure your description includes "closes #xxxx",
    "fixes #xxxx" or "resolves #xxxx" so that GitHub automatically closes the related
    issue when the PR is merged.

    If you are adding/modifying/removing any command or utility script, please also
    make sure to add/modify/remove any unit tests from the tests
    directory as appropriate.

    If you are modifying or removing an existing 'show', 'config' or 'sonic-clear'
    subcommand, or you are adding a new subcommand, please make sure you also
    update the Command Line Reference Guide (doc/Command-Reference.md) to reflect
    your changes.

    Please provide the following information:
-->
#### Why I did it
In some scenarios, after install plugin then power cycle, file content might lost.
Before power cycle, file size is 205, also can found register function in python file, but after power cycle, this file size is 0, so assume this is caused by page cache didn't write back to disk on time, when power cycle happen.
Before power cycle:
```bash
2026 Feb  3 10:34:16.156531 sonic-testbed INFO  [DIAGNOSTIC] Starting CLI plugins installation for package: cpu-report
2026 Feb  3 10:34:16.157013 sonic-testbed INFO  [DIAGNOSTIC] Installing CLI plugin: package=cpu-report, command=show, src=/show.py, dst=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.157177 sonic-testbed INFO  [DIAGNOSTIC] Starting extract: image=sha256:1230c222517c88863253c94dba34a788b580604618373fff24ab737a7d519c3f, src=/show.py, dst=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.267834 sonic-testbed INFO  [DIAGNOSTIC] Tar buffer size: 2048 bytes, MD5: b0b48780efda61d230dc2e3592cc3ba6
2026 Feb  3 10:34:16.268709 sonic-testbed INFO  [DIAGNOSTIC] Tar member: name=show.py, size=205, isfile=True
2026 Feb  3 10:34:16.269652 sonic-testbed INFO  [DIAGNOSTIC] File extracted successfully: path=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py, size=205, MD5=f2f3ca5258fd0685adf2cc44567934fb, elapsed=0.112s
2026 Feb  3 10:34:16.270313 sonic-testbed INFO  [DIAGNOSTIC] Python syntax validation: PASS for /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.270820 sonic-testbed INFO  [DIAGNOSTIC] Plugin file verification after extract: path=/usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py, size=205, MD5=f2f3ca5258fd0685adf2cc44567934fb, mtime=1684332898.0, extract_time=0.113s
2026 Feb  3 10:34:16.271351 sonic-testbed INFO  [DIAGNOSTIC] Python syntax check: PASS for /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.271638 sonic-testbed INFO  [DIAGNOSTIC] Found "def register" in plugin file: /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
2026 Feb  3 10:34:16.271918 sonic-testbed INFO  [DIAGNOSTIC] Completed CLI plugins installation for package: cpu-report, elapsed=0.115s
```

After power cycle:
```bash
admin@sonic-testbed:~$ show version 2>&1
failed to import plugin show.plugins.cpu-report: module 'show.plugins.cpu-report' has no attribute 'register'

# file size is 0
admin@sonic-testbed:~$ ls -lih /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
830572 -rw-r--r-- 1 root root 0 May 17  2023 /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
# md5sum is different with previous
admin@sonic-testbed:~$ sudo md5sum /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
d41d8cd98f00b204e9800998ecf8427e  /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
# file is empty
admin@sonic-testbed:~$ sudo stat /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
  File: /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 0,27    Inode: 830572      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2026-02-03 10:34:16.266593882 +0200
Modify: 2023-05-17 17:14:58.000000000 +0300
Change: 2026-02-03 10:34:16.262593831 +0200
 Birth: 2026-02-03 10:34:16.262593831 +0200
admin@sonic-testbed:~$ cat /usr/local/lib/python3.13/dist-packages/show/plugins/cpu-report.py
admin@sonic-testbed:~$
```

#### What I did
Fix intermittent plugin corruption after power cycle by adding `os.sync()` to flush filesystem buffers after all CLI plugins are installed. This prevents incomplete plugin files that cause `'module has no attribute 'register''` errors in show commands after system reboot.

#### How I did it
Added `os.sync()` system call in `PackageManager._install_cli_plugins()` method after all CLI plugin files are extracted and installed. This ensures that:
1. All plugin file data is flushed from the OS page cache to disk
2. File metadata and data are both persisted before the method returns
3. Plugin files remain intact even if an abrupt power loss occurs shortly after installation

#### How to verify it
1. Install cpu-report package: `sonic-package-manager install cpu-report==1.0.0 -y`
2. Enable feature: `config feature state cpu-report enabled`
3. Upgrade package: `sonic-package-manager install cpu-report==1.0.7 -y`
4. Upgrade again: `sonic-package-manager install cpu-report==1.0.8 -y`
5. Immediately perform power cycle
6. After reboot, run: `show version`
If there is problem, error is: failed to import plugin show.plugins.cpu-report: module 'show.plugins.cpu-report' has no attribute 'register'.

Signed-off-by: Sonic Build Admin <[email protected]>
@mssonicbld
Copy link
Collaborator Author

Original PR: #4251

@mssonicbld
Copy link
Collaborator Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld mssonicbld merged commit 4cd422a into sonic-net:202511 Mar 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant