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

Fix race condition during loading tokens #1704

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public protocol SolanaTokensService: TokenRepository {
}

public actor KeyAppSolanaTokenRepository: SolanaTokensService {
static let version: Int = 1
static let version: Int = 2

struct Database: Codable, Hashable {
var timestamps: Date?
Expand All @@ -36,6 +36,8 @@ public actor KeyAppSolanaTokenRepository: SolanaTokensService {

var status: Status = .initialising

var setupTask: Task<Void, Never>?

public init(provider: KeyAppTokenProvider, errorObserver: ErrorObserver) {
self.provider = provider
self.errorObserver = errorObserver
Expand All @@ -46,50 +48,63 @@ public actor KeyAppSolanaTokenRepository: SolanaTokensService {
return
}

// Load from local storage
if status == Status.initialising {
if let encodedData = try? await storage.load(for: filename) {
if let database = try? JSONDecoder().decode(Database.self, from: encodedData) {
if let migratedDatabase = migrate(database: database) {
self.database = migratedDatabase
setupStaticToken(data: migratedDatabase.data)
if let setupTask {
await setupTask.value
return
}

setupTask = Task {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since setupTask store strong self and self stores setupTask, there will be retain cycle here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

setupTask = nil after running

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why don't we just capture [weak self]?

// Load from local storage
if status == Status.initialising {
if let encodedData = try? await storage.load(for: filename) {
if let database = try? JSONDecoder().decode(Database.self, from: encodedData) {
if let migratedDatabase = migrate(database: database) {
self.database = migratedDatabase
setupStaticToken(data: migratedDatabase.data)
}
}
}
}
}

do {
let result = try await provider.getSolanaTokens(modifiedSince: database.timestamps)
switch result {
case .noChanges:
status = .ready
return
case let .result(result):
// Update database
database.timestamps = result.timestamp
let tokens = result.tokens.map { token in
(token.mintAddress, token)
do {
let result = try await provider.getSolanaTokens(modifiedSince: database.timestamps)
switch result {
case .noChanges:
status = .ready
return
case let .result(result):
// Update database
database.timestamps = result.timestamp
let tokens = result.tokens.map { token in
(token.mintAddress, token)
}
let data = Dictionary(tokens, uniquingKeysWith: { lhs, _ in lhs })
database.version = Self.version
database.data = data
setupStaticToken(data: data)
status = .ready
}
let data = Dictionary(tokens, uniquingKeysWith: { lhs, _ in lhs })
database.version = Self.version
database.data = data
setupStaticToken(data: data)
status = .ready
}

if let encodedData = try? JSONEncoder().encode(database) {
try? await storage.save(for: filename, data: encodedData)
if let encodedData = try? JSONEncoder().encode(database) {
try await storage.save(for: filename, data: encodedData)
}
} catch {
errorObserver.handleError(error)
}
} catch {
errorObserver.handleError(error)
}

await setupTask?.value
setupTask = nil
}

func migrate(database: Database) -> Database? {
switch database.version {
case .none:
return nil
case 1:
// Clear all data to ensure user has been updated with the latest data by using new algorithm.
return .init(data: [:])
case 2:
return database
default:
return database
Expand Down
Loading