Skip to content

[New Lint Request] If possible, suggest releasing sync locks early #9399

Open
@c410-f3r

Description

@c410-f3r

Problem

Code that heavily depends on synchronous primitives like Mutex can suffer unnecessary resource contention if the lock is only released at the end of its scope through Drop but could in fact be released early.

fn example_1() {
  let locked = some_sync_resource.lock();
  let owned_rslt = locked.do_stuff_with_resource();
  // Only `owned_rslt` is needed but `locked` is still held
  do_heavy_computation_that_takes_time(owned_rslt);
}

fn example_2() {
  let locked = some_sync_resource.lock();
  let reference = locked.get_inner_resource_ref();
  let _ = reference.do_stuff();
  let _ = reference.do_other_things();
  // From this point forward, `locked` is not needed anymore
  let _ = compute_foo();
  let _ = compute_bar();
  let _ = compute_baz();
}

Request

A new lint called sync_lock_drop that asks for an explicit "inline" expression or an explicit drop call. The above snippets would then be rewritten as follows:

fn example_1() {
  let owned_rslt = some_sync_resource.lock().do_stuff_with_resource();
  do_heavy_computation_that_takes_time(owned_rslt);
}

fn example_2() {
  let locked = some_sync_resource.lock();
  let reference = locked.get_inner_resource_ref();
  let _ = reference.do_stuff();
  let _ = reference.do_other_things();
  drop(locked);
  let _ = compute_foo();
  let _ = compute_bar();
  let _ = compute_baz();
}

Implementation

It is trivial to detect synchronous primitives of the standard library but third-party implementations are tricky to detect. What should be done here? Hard code a list of well-known crates or allow users to provide a list of custom types?

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsE-hardCall for participation: This a hard problem and requires more experience or effort to work onL-pedanticLint: Belongs in the pedantic lint groupL-restrictionLint: Belongs in the restriction lint group

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions