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

fs_lock:Check the nwaiter when deleting a bucket #13826

Merged
merged 1 commit into from
Oct 5, 2024

Conversation

crafcat7
Copy link
Contributor

@crafcat7 crafcat7 commented Oct 5, 2024

Summary

Fixed the problem of releasing the bucket prematurely in multi-threaded flock scenarios by #13821

How to reproduce the problem
A thread setlk
B thread setlk_wait
A thread releases lock but fails to determine if nwaiter causes the bucket to be released prematurely post B thread causes crash due to heap use after free

Impact

Is a new feature added?: NO
Impact on build: NO
Impact on hardware: NO, this change does not specifically target any particular hardware architectures or boards.
Impact on documentation: NO,This patch does not introduce any new features
Impact on compatibility: This implementation aims to be backward compatible. No existing functionality is expected to be broken.

Testing

Build Host(s): Linux x86
Target(s): sim/nsh

We can through the two process USES case to verify the problem as follows

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>

#define FILENAME "/data/lockfile.txt"

int main() {
    int fd;

    fd = open(FILENAME, O_CREAT | O_RDWR, 0666);
    if (fd == -1) {
        perror("Failed to open file");
        exit(EXIT_FAILURE);
    }

    printf("Main thread: Trying to acquire write lock...\n");
    if (flock(fd, LOCK_EX) == -1) {
        perror("Failed to acquire write lock");
        close(fd);
        exit(EXIT_FAILURE);
    }
    printf("Main thread: Acquired write lock, holding for 5 seconds...\n");
    sleep(5);

    printf("Main thread: Releasing write lock...\n");
    if (flock(fd, LOCK_UN) == -1) {
        perror("Failed to release write lock");
        close(fd);
        exit(EXIT_FAILURE);
    }

    close(fd);
    printf("All locks released and file closed.\n");

    return 0;
}

Summary:
  Fixed the problem of releasing the bucket prematurely in multi-threaded flock scenarios.

A thread setlk
B thread setlk_wait
A thread releases lock but fails to determine if nwaiter causes the bucket to be released prematurely
post B thread causes crash due to heap use after free

apache#13821

Signed-off-by: chenrun1 <[email protected]>
@github-actions github-actions bot added Area: File System File System issues Size: XS The size of the change in this PR is very small labels Oct 5, 2024
@crafcat7 crafcat7 mentioned this pull request Oct 5, 2024
1 task
@xiaoxiang781216 xiaoxiang781216 linked an issue Oct 5, 2024 that may be closed by this pull request
1 task
@xiaoxiang781216 xiaoxiang781216 merged commit 8f9857b into apache:master Oct 5, 2024
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: File System File System issues Size: XS The size of the change in this PR is very small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] flock unlocking issues
2 participants