Skip to content

Conversation

@HoshimiP
Copy link

adjust linker script to ensure tests run

@AsakuraMizu
Copy link
Contributor

Let me fix the CI issue first.

_percpu_start = .;
_percpu_end = _percpu_start + SIZEOF(.percpu);
.percpu 0x0 (NOLOAD) : AT(_percpu_start) {
.percpu (NOLOAD) : AT(_percpu_start) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it also fix issues like arceos-org/percpu#5 and arceos-org/percpu#12? If so, you may also contribute it to percpu.

Copy link
Author

Choose a reason for hiding this comment

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

This change would make the load address no longer 0x0, which causes the assertions in the tests to fail. The corresponding tests would also need to be adjusted.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. You may adjust the test codes accordingly.

Copy link
Contributor

@AsakuraMizu AsakuraMizu left a comment

Choose a reason for hiding this comment

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

I like the threading and panic::catch_unwind part. Though there are still issues:

  1. There is some overlap with existing tests; let's merge them.
  2. What is the purpose of serial_test? If it's due to data conflicts, please isolate the data used by the test cases.

tests/tests.rs Outdated

#[allow(dead_code)]
struct Counter {
id: usize,
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the purpose of id?

tests/tests.rs Outdated
}

scope_local! {
static COUNTER: Arc<Counter> = Arc::new(Counter{id: 0});
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the purpose of Arc?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you may want to use an Arc<AtomicUsize>?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances test coverage for scope-local functionality by adding three new test scenarios and fixes a linker script issue to ensure tests run properly. The changes add comprehensive testing for multi-threading, panic handling, thread isolation, and nested scope behavior.

Key Changes:

  • Added multi-threading and panic recovery tests to the existing shared test
  • Introduced new isolation test to verify thread-local scope independence
  • Added new nested test to verify scope stacking behavior
  • Fixed linker script by removing explicit 0x0 VMA address from .percpu section

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/scope_local.rs Adds panic and thread imports, extends shared test with multi-threading and panic scenarios, adds isolation and nested test functions to verify thread safety and scope nesting
percpu.x Removes explicit 0x0 VMA address from .percpu section definition to allow relative positioning and ensure tests run correctly

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


assert_eq!(Arc::strong_count(&SHARED), 1);

let handles: Vec<_> = (0..9)
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

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

The range 0..9 creates 9 threads, which appears inconsistent with the isolation test that uses 0..10 for 10 threads. Consider using 0..10 here as well for consistency, or document why 9 threads is the intended count.

Copilot uses AI. Check for mistakes.
Comment on lines +94 to +95

ActiveScope::set_global();
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this line? I remember percpu delegates to thread local if target_os = "linux"

Copy link
Author

Choose a reason for hiding this comment

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

Removing this line will cause a SIGSEGV

Copy link
Contributor

Choose a reason for hiding this comment

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

Removing this line will cause a SIGSEGV

That's strange, and may indicate a bug in our design.

}

#[test]
fn isolation() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding a test that multiple threads share one scope?

@AsakuraMizu
Copy link
Contributor

I tested it several times locally and found the results to be unstable. This is likely related to an issue with percpu. Let's see if arceos-org/percpu#16 fixes it.

Comment on lines 57 to 105

let handles: Vec<_> = (0..10)
.map(|_| {
thread::spawn(move || {
let mut scope = Scope::new();
*SHARED.scope_mut(&mut scope) = SHARED.clone();
assert!(Arc::strong_count(&SHARED) >= 2);
})
})
.collect();

for h in handles {
h.join().unwrap();
}

assert_eq!(Arc::strong_count(&SHARED), 1);

{
let mut scope = Scope::new();
*SHARED.scope_mut(&mut scope) = SHARED.clone();
let scope = Arc::new(scope);

let handles: Vec<_> = (0..10)
.map(|_| {
let scope = scope.clone();
thread::spawn(move || {
unsafe { ActiveScope::set(&scope) };
assert_eq!(Arc::strong_count(&SHARED), 2);
assert_eq!(*SHARED, Arc::new("qwq".to_string()));
ActiveScope::set_global();
})
})
.collect();

for h in handles {
h.join().unwrap();
}
}

assert_eq!(Arc::strong_count(&SHARED), 1);

let panic = panic::catch_unwind(|| {
let mut scope = Scope::new();
*SHARED.scope_mut(&mut scope) = SHARED.clone();
panic!("panic");
});
assert!(panic.is_err());

assert_eq!(Arc::strong_count(&SHARED), 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

This test function is too large now. Let's break it down.

@AsakuraMizu
Copy link
Contributor

Waiting for percpu to release a new version.

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.

2 participants