diff --git a/lib/SimpleSAML/Session.php b/lib/SimpleSAML/Session.php
index 404b54819721f8777b3980d8661bdaae70f55eeb..f3ecffc92d9f7e071e0c4ccde95e15543bf09fd0 100644
--- a/lib/SimpleSAML/Session.php
+++ b/lib/SimpleSAML/Session.php
@@ -177,7 +177,6 @@ class SimpleSAML_Session {
 		$this->trackid = substr(md5(uniqid(rand(), true)), 0, 10);
 
 		$this->dirty = TRUE;
-		$this->addShutdownFunction();
 
 		/* Initialize data for session check function if defined */
 		$globalConfig = SimpleSAML_Configuration::getInstance();
@@ -189,6 +188,32 @@ class SimpleSAML_Session {
 	}
 
 
+    /**
+     * Destructor for this class. It will save the session to the session handler
+     * in case the session has been marked as dirty. Do nothing otherwise.
+     */
+    public function __destruct() {
+        if(!$this->dirty) {
+            /* Session hasn't changed - don't bother saving it. */
+            return;
+        }
+
+        $this->dirty = FALSE;
+
+        $sh = SimpleSAML_SessionHandler::getSessionHandler();
+
+        try {
+            $sh->saveSession($this);
+        } catch (Exception $e) {
+            if (!($e instanceof SimpleSAML_Error_Exception)) {
+                $e = new SimpleSAML_Error_UnserializableException($e);
+            }
+            SimpleSAML_Logger::error('Unable to save session.');
+            $e->logError();
+        }
+    }
+
+
 	/**
 	 * Upgrade this session object to use the $authData property.
 	 *
@@ -247,7 +272,6 @@ class SimpleSAML_Session {
 	 * This function is called after this class has been deserialized.
 	 */
 	public function __wakeup() {
-		$this->addShutdownFunction();
 
 		/* TODO: Remove for version 1.8. */
 		if ($this->authData === NULL) {
@@ -1166,42 +1190,6 @@ class SimpleSAML_Session {
 	}
 
 
-	/**
-	 * Save the session to the session handler.
-	 *
-	 * This function will check the dirty-flag to check if the session has changed.
-	 */
-	public function saveSession() {
-
-		if(!$this->dirty) {
-			/* Session hasn't changed - don't bother saving it. */
-			return;
-		}
-
-		$this->dirty = FALSE;
-
-		$sh = SimpleSAML_SessionHandler::getSessionHandler();
-
-		try {
-			$sh->saveSession($this);
-		} catch (Exception $e) {
-			if (!($e instanceof SimpleSAML_Error_Exception)) {
-				$e = new SimpleSAML_Error_UnserializableException($e);
-			}
-			SimpleSAML_Logger::error('Unable to save session.');
-			$e->logError();
-		}
-	}
-
-
-	/**
-	 * Add a shutdown function for saving this session object on exit.
-	 */
-	private function addShutdownFunction() {
-		register_shutdown_function(array($this, 'saveSession'));
-	}
-
-
 	/**
 	 * Set the logout state for this session.
 	 *