Skip to content

Ensures that using a cursor after close has no effect.#803

Merged
tgregg merged 1 commit intomasterfrom
uneventful-close
Apr 23, 2024
Merged

Ensures that using a cursor after close has no effect.#803
tgregg merged 1 commit intomasterfrom
uneventful-close

Conversation

@tgregg
Copy link
Contributor

@tgregg tgregg commented Apr 23, 2024

Description of changes:

The IonCursor interface extends Closeable, which includes documentation on close that states that calling close after the resource is already closed has no effect. We do not define the behavior of the cursor when methods other than close are called after the cursor is closed, but I think it is reasonable for the cursor to remain in a NEEDS_DATA state, indicating that it is not positioned on a value.

Before the proposed changes, the added tests would fail with NullPointerException when the cursor attempted to access its buffer, which is set to null during close. Entering the TERMINATED state in "slow" mode allows the cursor to make use of existing termination checks that gate all of the slow mode APIs.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

/**
* Dummy state that indicates the cursor has been terminated and that additional API calls will have no effect.
*/
private static final RefillableState TERMINATED_STATE = new RefillableState(null, -1, -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems sensible, even if it's not essential, for the TERMINATED_STATE to have a pre-populated state of TERMINATED.

Suggested change
private static final RefillableState TERMINATED_STATE = new RefillableState(null, -1, -1);
private static final RefillableState TERMINATED_STATE = new RefillableState(null, -1, -1);
static {
TERMINATED_STATE.state = State.TERMINATED;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

terminate();
// Use a unified code path for all cursors after close. This path forces a termination check before accessing
// the input stream or buffer.
isSlowMode = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should isSlowMode = true be in terminate() so that it is also set on other conditions that result in a terminated state?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@tgregg tgregg force-pushed the uneventful-close branch from fdf9ee5 to bc40ac6 Compare April 23, 2024 18:10
@tgregg tgregg force-pushed the uneventful-close branch from bc40ac6 to e408153 Compare April 23, 2024 18:11
@tgregg tgregg merged commit 5ee9f7e into master Apr 23, 2024
@tgregg tgregg deleted the uneventful-close branch April 23, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants