am 840b3bd6: Merge "Fix provider leak in PFD" into klp-dev

* commit '840b3bd61185456ccb574c2be115ea78ec418327':
  Fix provider leak in PFD
This commit is contained in:
Amith Yamasani
2013-09-18 11:02:59 -07:00
committed by Android Git Automerger
2 changed files with 37 additions and 21 deletions

View File

@@ -2316,8 +2316,7 @@ public abstract class ContentResolver {
}
@Override
public void close() throws IOException {
super.close();
public void releaseResources() {
if (!mProviderReleased) {
ContentResolver.this.releaseProvider(mContentProvider);
mProviderReleased = true;

View File

@@ -564,7 +564,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
@Override
public void close() throws IOException {
if (mWrapped != null) {
mWrapped.close();
try {
mWrapped.close();
} finally {
releaseResources();
}
} else {
closeWithStatus(Status.OK, null);
}
@@ -579,7 +583,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
*/
public void closeWithError(String msg) throws IOException {
if (mWrapped != null) {
mWrapped.closeWithError(msg);
try {
mWrapped.closeWithError(msg);
} finally {
releaseResources();
}
} else {
if (msg == null) {
throw new IllegalArgumentException("Message must not be null");
@@ -588,17 +596,22 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
}
}
private void closeWithStatus(int status, String msg) throws IOException {
if (mWrapped != null) {
mWrapped.closeWithStatus(status, msg);
} else {
if (mClosed) return;
mClosed = true;
mGuard.close();
// Status MUST be sent before closing actual descriptor
writeCommStatusAndClose(status, msg);
IoUtils.closeQuietly(mFd);
}
private void closeWithStatus(int status, String msg) {
if (mClosed) return;
mClosed = true;
mGuard.close();
// Status MUST be sent before closing actual descriptor
writeCommStatusAndClose(status, msg);
IoUtils.closeQuietly(mFd);
releaseResources();
}
/**
* Called when the fd is being closed, for subclasses to release any other resources
* associated with it, such as acquired providers.
* @hide
*/
public void releaseResources() {
}
private byte[] getOrCreateStatusBuffer() {
@@ -793,6 +806,9 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
@Override
protected void finalize() throws Throwable {
if (mWrapped != null) {
releaseResources();
}
if (mGuard != null) {
mGuard.warnIfOpen();
}
@@ -822,7 +838,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
@Override
public void writeToParcel(Parcel out, int flags) {
if (mWrapped != null) {
mWrapped.writeToParcel(out, flags);
try {
mWrapped.writeToParcel(out, flags);
} finally {
releaseResources();
}
} else {
out.writeFileDescriptor(mFd);
if (mCommFd != null) {
@@ -832,11 +852,8 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
out.writeInt(0);
}
if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0 && !mClosed) {
try {
// Not a real close, so emit no status
closeWithStatus(Status.SILENCE, null);
} catch (IOException e) {
}
// Not a real close, so emit no status
closeWithStatus(Status.SILENCE, null);
}
}
}