Skip to content

Commit

Permalink
Fix NPE when server has been closed asynchronous.
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonlu committed Dec 5, 2017
1 parent 53ac423 commit 2f708e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public void onClose(boolean readComplete, ByteArrayOutputStream outputStream) {
SonicUtils.log(TAG, Log.ERROR, "session(" + session.sId + "), onClose error:" + e.getMessage() + ".");
}
}
session.onServerClosed(readComplete);
session.onServerClosed(this, readComplete);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,10 @@ protected boolean switchState(int fromState, int toState, boolean notify) {
* If the html is read complete, sonic will separate the html to template and data, and save these
* data.
*
* @param sonicServer The actual server connection of current SonicSession.
* @param readComplete Whether the html is read complete.
*/
public void onServerClosed(final boolean readComplete) {
public void onServerClosed(final SonicServer sonicServer, final boolean readComplete) {
// if the session has been destroyed, exit directly
if(isDestroyedOrWaitingForDestroy()) {
return;
Expand All @@ -757,8 +758,8 @@ public void onServerClosed(final boolean readComplete) {

//Separate and save html.
if (readComplete) {
String cacheOffline = server.getResponseHeaderField(SonicSessionConnection.CUSTOM_HEAD_FILED_CACHE_OFFLINE);
if (SonicUtils.needSaveData(config.SUPPORT_CACHE_CONTROL, cacheOffline, server.getResponseHeaderFields())) {
String cacheOffline = sonicServer.getResponseHeaderField(SonicSessionConnection.CUSTOM_HEAD_FILED_CACHE_OFFLINE);
if (SonicUtils.needSaveData(config.SUPPORT_CACHE_CONTROL, cacheOffline, sonicServer.getResponseHeaderFields())) {
SonicUtils.log(TAG, Log.INFO, "session(" + sId + ") onClose:offline->" + cacheOffline + " , post separateAndSaveCache task.");
SonicEngine.getInstance().getRuntime().postTaskToThread(new Runnable() {
@Override
Expand All @@ -768,7 +769,7 @@ public void run() {
return;
}

String htmlString = server.getResponseData(false);
String htmlString = sonicServer.getResponseData(false);
if (SonicUtils.shouldLog(Log.DEBUG)) {
SonicUtils.log(TAG, Log.DEBUG, "session(" + sId + ") onClose:htmlString size:"
+ (!TextUtils.isEmpty(htmlString) ? htmlString.length() : 0));
Expand Down Expand Up @@ -1135,15 +1136,16 @@ protected void destroy(boolean force) {
clearSessionData();

if (force || canDestroy()) {
sessionState.set(STATE_DESTROY);
synchronized (sessionState) {
sessionState.notify();
}

if (null != server && !force) {
server.disconnect();
server = null;
}

sessionState.set(STATE_DESTROY);
synchronized (sessionState) {
sessionState.notify();
}
notifyStateChange(curState, STATE_DESTROY, null);

mainHandler.removeMessages(SESSION_MSG_FORCE_DESTROY);
Expand Down

0 comments on commit 2f708e6

Please sign in to comment.