How to release a resource protected by a mutex (std.Io)

I have a resource which must be protected by a mutex, but I need to be able to release it. How do I follow “resource deallocation must succeed” if a mutex lock is failable with error.Canceled?

pub fn releaseTransactions(self: *Port, io: std.Io, transactions: []Transaction) void {
    try self.transactions_mutex.lock(io); // < ------ I cant try here!?
    defer self.transactions_mutex.unlock();
    for (transactions) |*transaction| {
        if (!transaction.data.released) {
            self.transactions.remove(&transaction.node);
            transaction.data.released = true;
        }
        assert(transaction.data.released == true);
    }
}

there is mutex.lockUncancelable which I think is for this.

3 Likes