[vlc-commits] [Git][videolan/vlc][master] 2 commits: macosx: Acquire token and generation count under same lock
Felix Paul Kühne (@fkuehne)
gitlab at videolan.org
Mon Nov 9 22:39:35 UTC 2026
Felix Paul Kühne pushed to branch master at VideoLAN / VLC
Commits:
90b38748 by Claudio at 2026-11-09T23:21:38+01:00
macosx: Acquire token and generation count under same lock
Fixes races during test
- - - - -
86d8de6a by Claudio at 2026-11-09T23:21:38+01:00
macosx: Test monitored folder refresh across filter changes
- - - - -
4 changed files:
- modules/gui/macosx/library/VLCLibraryModel.m
- modules/gui/macosx/tests/VLCLibraryModelIntegrationTest.m
- modules/gui/macosx/tests/VLCLibraryModelTestSupport.h
- modules/gui/macosx/tests/VLCLibraryModelTestSupport.m
Changes:
=====================================
modules/gui/macosx/library/VLCLibraryModel.m
=====================================
@@ -618,8 +618,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
});
return;
}
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfShows)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfShows)
+ generation:&generation];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
NSDictionary<NSNumber *, NSNumber *> * const showIDs = [self showIDsByEpisodeIDForShows:shows];
if (showIDs == nil) {
@@ -733,9 +734,13 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
}
- (NSObject *)beginCacheUpdateForGetter:(SEL)getter
+ generation:(NSUInteger *)generation
{
NSObject * const token = NSObject.new;
@synchronized (_cacheUpdateTokens) {
+ // A cache drop between reading the version and saving the token
+ // can cause both this query and its replacement to be discarded.
+ *generation = self.cacheGeneration;
_cacheUpdateTokens[[NSValue valueWithPointer:getter]] = token;
[_pendingCacheUpdates addObject:[NSValue valueWithPointer:getter]];
}
@@ -831,8 +836,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfAudioMedia
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedAudioMedia)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedAudioMedia)
+ generation:&generation];
NSString * const filterString = [self.filterString copy];
const vlc_ml_query_params_t capturedParams = [self queryParams];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
@@ -885,8 +891,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfArtists
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedArtists)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedArtists)
+ generation:&generation];
NSString * const filterString = [self.filterString copy];
const vlc_ml_query_params_t capturedParams = [self queryParams];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
@@ -949,8 +956,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfAlbums
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedAlbums)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedAlbums)
+ generation:&generation];
NSString * const filterString = [self.filterString copy];
const vlc_ml_query_params_t capturedParams = [self queryParams];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
@@ -1010,8 +1018,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfGenres
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedGenres)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedGenres)
+ generation:&generation];
NSString * const filterString = [self.filterString copy];
const vlc_ml_query_params_t capturedParams = [self queryParams];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
@@ -1072,8 +1081,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfVideoMedia
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedVideoMedia)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedVideoMedia)
+ generation:&generation];
NSString * const filterString = [self.filterString copy];
const vlc_ml_query_params_t capturedParams = [self queryParams];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
@@ -1230,8 +1240,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfRecentMedia
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedRecentMedia)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedRecentMedia)
+ generation:&generation];
[self getListOfRecentMediaOfType:VLC_ML_MEDIA_TYPE_VIDEO
withCountLimit:_recentMediaLimit
withCompletion:^(NSArray * const mediaArray) {
@@ -1273,8 +1284,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfRecentAudioMedia
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedRecentAudioMedia)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedRecentAudioMedia)
+ generation:&generation];
[self getListOfRecentMediaOfType:VLC_ML_MEDIA_TYPE_AUDIO
withCountLimit:_recentAudioMediaLimit
withCompletion:^(NSArray * const mediaArray) {
@@ -1333,8 +1345,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
NSNotificationName const notification = audio
? VLCLibraryModelRecentlyAddedAudioMediaListReset
: VLCLibraryModelRecentlyAddedVideoMediaListReset;
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:getter];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:getter
+ generation:&generation];
NSString * const filterString = [self.filterString copy];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
vlc_ml_query_params_t queryParams = vlc_ml_query_params_create();
@@ -1400,8 +1413,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfShows
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfShows)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfShows)
+ generation:&generation];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
vlc_ml_show_list_t * const p_show_list = vlc_ml_list_shows(self->_p_mediaLibrary, NULL);
if (p_show_list == NULL) {
@@ -1515,8 +1529,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfMovies
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfMovies)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfMovies)
+ generation:&generation];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
vlc_ml_media_list_t * const p_movie_list = vlc_ml_list_movies(self->_p_mediaLibrary, NULL);
if (p_movie_list == NULL) {
@@ -1614,8 +1629,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfGroups
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfGroups)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfGroups)
+ generation:&generation];
NSString * const filterString = [self.filterString copy];
const vlc_ml_query_params_t capturedParams = [self queryParams];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
@@ -1703,8 +1719,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
- (void)resetCachedListOfMonitoredFolders
{
- const NSUInteger generation = self.cacheGeneration;
- NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfMonitoredFolders)];
+ NSUInteger generation;
+ NSObject * const token = [self beginCacheUpdateForGetter:@selector(cachedListOfMonitoredFolders)
+ generation:&generation];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{
vlc_ml_folder_list_t *pp_entrypoints = vlc_ml_list_entry_points(self->_p_mediaLibrary, NULL);
if (pp_entrypoints == NULL) {
=====================================
modules/gui/macosx/tests/VLCLibraryModelIntegrationTest.m
=====================================
@@ -1745,6 +1745,67 @@
@"Each monitored URL must have exactly one active observer");
}
+- (void)testMonitoredFolderRefreshSurvivesFilterChangeBeforeTokenRegistration
+{
+ // Pause a background refresh, change the filter, and start a replacement
+ // before letting the background refresh mark itself as the latest request.
+ // Before the fix, the paused refresh had already read the old cache version.
+ // Its result was rejected as outdated, and the replacement's result was
+ // rejected because it was no longer the latest request. Neither filled the
+ // cache or notified. Force that order and require the folders and notification.
+ XCTestExpectation * const loaded = [self expectationForNotification:VLCLibraryModelListOfMonitoredFoldersUpdated
+ object:self.model
+ handler:nil];
+ [self waitForExpectations:@[ loaded ] timeout:5.];
+ VLCLibraryModelTestQueryGate * const model = self.model;
+ [self stopModelCallback];
+ XCTestExpectation * const settled = [self expectationWithDescription:@"Initial folder queries finished"];
+ [model finishPendingQueriesWithCompletion:^{ [settled fulfill]; }];
+ [self waitForExpectations:@[ settled ] timeout:5.];
+ NSSet<NSString *> * const expectedMRLs =
+ [NSSet setWithArray:[model.cachedListOfMonitoredFolders valueForKey:@"MRL"]];
+ XCTAssertGreaterThan(expectedMRLs.count, (NSUInteger)0);
+
+ XCTestExpectation * const paused = [self expectationWithDescription:@"Background folder refresh paused before token registration"];
+ XCTestExpectation * const registered = [self expectationWithDescription:@"Background folder refresh token registered"];
+ [model holdNextCacheUpdateForGetter:@selector(cachedListOfMonitoredFolders)
+ handler:^{ [paused fulfill]; }
+ completionHandler:^{ [registered fulfill]; }];
+ [self addTeardownBlock:^{ [model releaseCacheUpdate]; }];
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
+ ^{
+ [model resetCachedListOfMonitoredFolders];
+ });
+ [self waitForExpectations:@[ paused ] timeout:5.];
+
+ XCTestExpectation * const dropped = [self expectationForNotification:VLCLibraryModelAllCachesDropped
+ object:model
+ handler:nil];
+ model.filterString = @"Folder refresh overlap";
+ [self waitForExpectations:@[ dropped ] timeout:5.];
+ // Keep the replacement from writing its result before the paused refresh resumes.
+ XCTestExpectation * const captured = [self expectationWithDescription:@"New folder result held"];
+ XCTestExpectation * const processed = [self expectationWithDescription:@"New folder write processed"];
+ [model holdNextQueryResultForGetter:@selector(cachedListOfMonitoredFolders)
+ handler:^{ [captured fulfill]; }
+ completionHandler:^{ [processed fulfill]; }];
+ [self addTeardownBlock:^{ [model releaseQuery]; }];
+ XCTestExpectation * const reloaded = [self expectationForNotification:VLCLibraryModelListOfMonitoredFoldersUpdated
+ object:model
+ handler:nil];
+ (void)model.listOfMonitoredFolders;
+ [self waitForExpectations:@[ captured ] timeout:5.];
+ XCTAssertNil(model.cachedListOfMonitoredFolders);
+
+ [model releaseCacheUpdate];
+ [self waitForExpectations:@[ registered ] timeout:5.];
+ [model releaseQuery];
+ [self waitForExpectations:@[ processed, reloaded ] timeout:5.];
+ NSSet<NSString *> * const reloadedMRLs =
+ [NSSet setWithArray:[model.cachedListOfMonitoredFolders valueForKey:@"MRL"]];
+ XCTAssertEqualObjects(reloadedMRLs, expectedMRLs);
+}
+
- (void)testDroppingCachesRejectsAPendingMonitoredFolderResult
{
[self assertDroppedMonitoredFoldersRejectsPendingWrite:NO];
=====================================
modules/gui/macosx/tests/VLCLibraryModelTestSupport.h
=====================================
@@ -63,7 +63,8 @@
token:(NSObject *)token;
- (NSArray *)readCacheOnQueue:(dispatch_queue_t)queue
withBlock:(NSArray *(^)(void))block;
-- (NSObject *)beginCacheUpdateForGetter:(SEL)getter;
+- (NSObject *)beginCacheUpdateForGetter:(SEL)getter
+ generation:(NSUInteger *)generation;
- (void)performCacheWriteForGeneration:(NSUInteger)generation
token:(NSObject *)token
getter:(SEL)getter
@@ -104,6 +105,11 @@ VLCMediaLibraryMediaItem *VLCLibraryModelTestMediaItem(vlc_ml_media_type_t type,
- (instancetype)initWithLibrary:(vlc_medialibrary_t *)library
observeEvents:(BOOL)observeEvents;
+// Pauses the next refresh before its token is registered.
+- (void)holdNextCacheUpdateForGetter:(SEL)getter
+ handler:(dispatch_block_t)handler
+ completionHandler:(dispatch_block_t)completionHandler;
+- (void)releaseCacheUpdate;
- (void)holdNextQueryResultForGetter:(SEL)getter
handler:(dispatch_block_t)handler
completionHandler:(dispatch_block_t)completionHandler;
=====================================
modules/gui/macosx/tests/VLCLibraryModelTestSupport.m
=====================================
@@ -262,6 +262,10 @@ VLCMediaLibraryMediaItem *VLCLibraryModelTestMediaItem(vlc_ml_media_type_t type,
@end
@implementation VLCLibraryModelTestQueryGate {
+ dispatch_semaphore_t _updateRelease;
+ SEL _updateGetter;
+ dispatch_block_t _updateCaptured;
+ dispatch_block_t _updateProcessed;
dispatch_semaphore_t _queryRelease;
SEL _getter;
NSObject *_heldToken;
@@ -350,6 +354,28 @@ VLCMediaLibraryMediaItem *VLCLibraryModelTestMediaItem(vlc_ml_media_type_t type,
XCTAssertTrue(self.initialCountsLoaded);
}
+- (void)holdNextCacheUpdateForGetter:(SEL)getter
+ handler:(dispatch_block_t)handler
+ completionHandler:(dispatch_block_t)completionHandler
+{
+ @synchronized (self) {
+ _updateRelease = dispatch_semaphore_create(0);
+ _updateGetter = getter;
+ _updateCaptured = [handler copy];
+ _updateProcessed = [completionHandler copy];
+ }
+}
+
+- (void)releaseCacheUpdate
+{
+ dispatch_semaphore_t release;
+ @synchronized (self) {
+ release = _updateRelease;
+ }
+ if (release != nil)
+ dispatch_semaphore_signal(release);
+}
+
- (void)holdNextQueryResultForGetter:(SEL)getter
handler:(dispatch_block_t)handler
completionHandler:(dispatch_block_t)completionHandler
@@ -364,12 +390,37 @@ VLCMediaLibraryMediaItem *VLCLibraryModelTestMediaItem(vlc_ml_media_type_t type,
}
- (NSObject *)beginCacheUpdateForGetter:(SEL)getter
+ generation:(NSUInteger *)generation
{
- NSObject * const token = [super beginCacheUpdateForGetter:getter];
+ dispatch_block_t captured = nil;
+ dispatch_block_t processed = nil;
+ dispatch_semaphore_t release = nil;
+ NSObject *pendingStart = nil;
+ @synchronized (self) {
+ if (_updateGetter == getter && _updateCaptured != nil) {
+ captured = _updateCaptured;
+ processed = _updateProcessed;
+ release = _updateRelease;
+ _updateCaptured = nil;
+ _updateProcessed = nil;
+ pendingStart = NSObject.new;
+ if (_pendingQueries == nil)
+ _pendingQueries = NSMutableSet.set;
+ [_pendingQueries addObject:pendingStart];
+ }
+ }
+ if (captured != nil) {
+ dispatch_async(dispatch_get_main_queue(), captured);
+ dispatch_semaphore_wait(release, dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC));
+ }
+ NSObject * const token = [super beginCacheUpdateForGetter:getter
+ generation:generation];
@synchronized (self) {
if (_pendingQueries == nil)
_pendingQueries = NSMutableSet.set;
[_pendingQueries addObject:token];
+ if (pendingStart != nil)
+ [_pendingQueries removeObject:pendingStart];
if (_queryCounts == nil)
_queryCounts = NSMutableDictionary.dictionary;
NSValue * const key = [NSValue valueWithPointer:getter];
@@ -384,6 +435,8 @@ VLCMediaLibraryMediaItem *VLCLibraryModelTestMediaItem(vlc_ml_media_type_t type,
_writeToken = token;
}
}
+ if (processed != nil)
+ dispatch_async(dispatch_get_main_queue(), processed);
return token;
}
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/6c35899343e5b67e6bf5cafcd996b6beb56de8a0...86d8de6a0aba00940fdbf0dbff307c44c84dae53
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/6c35899343e5b67e6bf5cafcd996b6beb56de8a0...86d8de6a0aba00940fdbf0dbff307c44c84dae53
You're receiving this email because of your account on code.videolan.org. Manage all notifications: https://code.videolan.org/-/profile/notifications | Help: https://code.videolan.org/help
More information about the vlc-commits
mailing list