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

Mild thread-safety issue #2

Closed
io7m opened this issue Mar 11, 2024 · 0 comments
Closed

Mild thread-safety issue #2

io7m opened this issue Mar 11, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@io7m
Copy link
Member

io7m commented Mar 11, 2024

If multiple threads attempt to close a collection at the same time, there's a chance that a NoSuchElementException will be raised:

@Override
  public void close()
    throws E
  {
    E e = null;

    while (!this.stack.isEmpty()) {
      final AutoCloseable resource = this.stack.pop();
      try {
        resource.close();
      } catch (final Exception re) {
        if (e == null) {
          e = this.exceptions.get();
        }
        e.addSuppressed(re);
      }
    }

    if (e != null) {
      throw e;
    }
  }

The this.stack.isEmpty() and this.stack.pop() lines result in a time-of-check-time-of-use issue. The stack could have been popped in between the two calls.

@io7m io7m added the bug Something isn't working label Mar 11, 2024
@io7m io7m self-assigned this Mar 11, 2024
@io7m io7m closed this as completed in ef94d6f Mar 11, 2024
io7m added a commit that referenced this issue Mar 11, 2024
Release: com.io7m.jmulticlose 1.1.1
Change: Fix a minor thread safety issue. (Ticket: #2)
io7m added a commit that referenced this issue Mar 11, 2024
Release: com.io7m.jmulticlose 1.1.1

Change: Fix a minor thread safety issue. (Ticket: #2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant