Bug 1820268 - make nsI{Input,Output}Stream::Close() idempotent r=xpcom-reviewers,nika,necko-reviewers,valentin

The `nsIAsync{Input,Output}Stream` base classes' Close() methods are
already documented to be idempotent. Do the same for their synchronous
counterparts, and make the necessary changes to ensure this.

The only known functional change here (modulo the removal of some
uninteresting logging) is to `nsFileInputStream`, which now avoids
calling `Tell()` if the stream is already closed. (The other early-exits
added here are formally redundant, but neither obviously nor robustly
so.)

This silences some console warning spam when running a debug build from
the command line.

Differential Revision: https://phabricator.services.mozilla.com/D171615
This commit is contained in:
Ray Kraesig 2023-03-29 17:01:32 +00:00
Родитель 3221c357fe
Коммит fbf8a777af
5 изменённых файлов: 21 добавлений и 1 удалений

Просмотреть файл

@ -491,6 +491,10 @@ RemoteLazyInputStream::Close() {
{
MutexAutoLock lock(mMutex);
if (mState == eClosed) {
return NS_OK;
}
MOZ_LOG(gRemoteLazyStreamLog, LogLevel::Debug,
("Close %s", Describe().get()));

Просмотреть файл

@ -868,6 +868,10 @@ nsBufferedOutputStream::Init(nsIOutputStream* stream, uint32_t bufferSize) {
NS_IMETHODIMP
nsBufferedOutputStream::Close() {
if (!mStream) {
return NS_OK;
}
nsresult rv1, rv2 = NS_OK;
rv1 = Flush();

Просмотреть файл

@ -159,6 +159,10 @@ nsFileStreamBase::GetFileDescriptor(PRFileDesc** _retval) {
}
nsresult nsFileStreamBase::Close() {
if (mState == eClosed) {
return NS_OK;
}
CleanUpOpen();
nsresult rv = NS_OK;
@ -476,6 +480,11 @@ nsFileInputStream::Init(nsIFile* aFile, int32_t aIOFlags, int32_t aPerm,
NS_IMETHODIMP
nsFileInputStream::Close() {
// If this stream has already been closed, do nothing.
if (mState == eClosed) {
return NS_OK;
}
// Get the cache position at the time the file was close. This allows
// NS_SEEK_CUR on a closed file that has been opened with
// REOPEN_ON_REWIND.
@ -484,7 +493,7 @@ nsFileInputStream::Close() {
nsFileStreamBase::Tell(&mCachedPosition);
}
// null out mLineBuffer in case Close() is called again after failing
// explicitly clear mLineBuffer in case this stream is reopened
mLineBuffer = nullptr;
return nsFileStreamBase::Close();
}

Просмотреть файл

@ -64,6 +64,8 @@ interface nsIInputStream : nsISupports
* ReadSegments to return 0 bytes read to indicate end-of-file. Any
* subsequent calls to Available or StreamStatus should throw
* NS_BASE_STREAM_CLOSED.
*
* Succeeds (without side effects) if already closed.
*/
void close();

Просмотреть файл

@ -56,6 +56,7 @@ interface nsIOutputStream : nsISupports
/**
* Close the stream. Forces the output stream to flush any buffered data.
* Any subsequent calls to StreamStatus should throw NS_BASE_STREAM_CLOSED.
* Succeeds without effect if already closed.
*
* @throws NS_BASE_STREAM_WOULD_BLOCK if unable to flush without blocking
* the calling thread (non-blocking mode only)