Skip to content

Commit 094ada8

Browse files
fix: thread safety (ConcurrentHashMap Android, weak closure iOS), tighten SDK constraint, add topics and lint rules (v1.0.6)
1 parent 4e31e7f commit 094ada8

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
---
99

10+
## [1.0.6] - 2026-02-28
11+
12+
### Fixed
13+
14+
- **Android: Thread safety — `taskTags`, `taskStatuses`, `taskStartTimes` replaced with `ConcurrentHashMap`** (`NativeWorkmanagerPlugin.kt`)
15+
- **Root cause:** All three maps used `mutableMapOf()` (`LinkedHashMap` under the hood), which is not thread-safe. Multiple coroutines launched from the plugin's `CoroutineScope` could read and write concurrently, causing silent data corruption or `ConcurrentModificationException` under load
16+
- **Fix:** Replaced with `java.util.concurrent.ConcurrentHashMap` which provides lock-free reads and segment-level locking for writes
17+
18+
- **iOS: Memory safety — `onTaskComplete` closure captures `instance` weakly** (`NativeWorkmanagerPlugin.swift`)
19+
- **Root cause:** `BGTaskSchedulerManager.shared.onTaskComplete` held a strong reference to the plugin instance via an implicit capture; the adjacent `progressDelegate` closure already used `[weak instance]` correctly but this one did not
20+
- **Fix:** Added `[weak instance]` capture and optional-chained the call site (`instance?.emitTaskEvent(...)`)
21+
22+
### Changed
23+
24+
- **pub.dev: Added `topics`**`background`, `workmanager`, `networking`, `files`, `cryptography` for better discoverability
25+
- **SDK constraint tightened**`sdk: '>=3.6.0 <4.0.0'` and `flutter: '>=3.27.0'` (was `>=3.10.0` / `>=3.3.0`; stricter constraint matches actual minimum required APIs and fixes pub.dev static analysis)
26+
- **`analysis_options.yaml` — added lint rules**: `cancel_subscriptions`, `close_sinks`, `avoid_returning_null_for_future`, `avoid_void_async`, `unawaited_futures`, `always_declare_return_types`, `avoid_relative_lib_imports`; added `missing_required_param: error` and `missing_return: error` analyzer settings
27+
28+
---
29+
1030
## [1.0.5] - 2026-02-22
1131

1232
### Added

analysis_options.yaml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
include: package:flutter_lints/flutter.yaml
22

3-
# Additional information about this file can be found at
4-
# https://dart.dev/guides/language/analysis-options
3+
analyzer:
4+
errors:
5+
missing_required_param: error
6+
missing_return: error
7+
exclude:
8+
- example/**
9+
- test/**
10+
11+
linter:
12+
rules:
13+
- cancel_subscriptions
14+
- close_sinks
15+
- avoid_returning_null_for_future
16+
- avoid_void_async
17+
- unawaited_futures
18+
- always_declare_return_types
19+
- avoid_relative_lib_imports

android/src/main/kotlin/dev/brewkits/native_workmanager/NativeWorkmanagerPlugin.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.work.WorkManager
1717
import dev.brewkits.kmpworkmanager.background.data.KmpHeavyWorker
1818
import dev.brewkits.kmpworkmanager.background.data.KmpWorker
1919
import dev.brewkits.kmpworkmanager.background.domain.*
20+
import java.util.concurrent.ConcurrentHashMap
2021
import java.util.concurrent.TimeUnit
2122
import dev.brewkits.native_workmanager.engine.FlutterEngineManager
2223
import dev.brewkits.kmpworkmanager.background.data.NativeTaskScheduler
@@ -70,17 +71,17 @@ class NativeWorkmanagerPlugin : FlutterPlugin, MethodCallHandler, KoinComponent
7071
private val scheduler: BackgroundTaskScheduler by inject()
7172
// TaskEventBus is an object singleton, accessed directly (not via Koin)
7273

73-
// Tag storage: taskId -> tag mapping
74-
private val taskTags = mutableMapOf<String, String>()
74+
// Tag storage: taskId -> tag mapping (ConcurrentHashMap for thread safety across coroutines)
75+
private val taskTags = ConcurrentHashMap<String, String>()
7576

76-
// Task status tracking: taskId -> status string
77-
private val taskStatuses = mutableMapOf<String, String>()
77+
// Task status tracking: taskId -> status string (ConcurrentHashMap for thread safety)
78+
private val taskStatuses = ConcurrentHashMap<String, String>()
7879

7980
// Debug mode flag
8081
private var debugMode = false
8182

82-
// Task start times for debug mode
83-
private val taskStartTimes = mutableMapOf<String, Long>()
83+
// Task start times for debug mode (ConcurrentHashMap for thread safety)
84+
private val taskStartTimes = ConcurrentHashMap<String, Long>()
8485

8586
companion object {
8687
private const val TAG = "NativeWorkmanagerPlugin"

ios/native_workmanager/Sources/native_workmanager/NativeWorkmanagerPlugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public class NativeWorkmanagerPlugin: NSObject, FlutterPlugin {
8989
InfoPlistValidator.printSetupGuideIfNeeded()
9090

9191
BGTaskSchedulerManager.shared.registerHandlers()
92-
BGTaskSchedulerManager.shared.onTaskComplete = { taskId, success, message in
93-
instance.emitTaskEvent(taskId: taskId, success: success, message: message)
92+
BGTaskSchedulerManager.shared.onTaskComplete = { [weak instance] taskId, success, message in
93+
instance?.emitTaskEvent(taskId: taskId, success: success, message: message)
9494
}
9595

9696
// Setup progress delegate for BackgroundSessionManager

pubspec.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
name: native_workmanager
22
description: "Background task manager for Flutter using platform-native APIs. Zero Flutter Engine overhead for I/O operations."
3-
version: 1.0.5
3+
version: 1.0.6
44
homepage: https://github.com/brewkits/native_workmanager
55
repository: https://github.com/brewkits/native_workmanager
66
issue_tracker: https://github.com/brewkits/native_workmanager/issues
77

8+
topics:
9+
- background
10+
- workmanager
11+
- networking
12+
- files
13+
- cryptography
14+
815
environment:
9-
sdk: '>=3.10.0 <4.0.0'
10-
flutter: '>=3.3.0'
16+
sdk: '>=3.6.0 <4.0.0'
17+
flutter: '>=3.27.0'
1118

1219
dependencies:
1320
flutter:

0 commit comments

Comments
 (0)