session_write_close

session_write_close(): bool

Writes $_SESSION data to storage and ends the current session (releasing the write lock); later $_SESSION changes are not persisted.

C C23

C23
#include <stdio.h>
#include <string.h>

struct KV {
    char key[32];
    char val[64];
};

static int kv_get(struct KV *store, int n, const char *key, char *out) {
    for (int i = 0; i < n; i++) {
        if (strcmp(store[i].key, key) == 0) {
            strcpy(out, store[i].val);
            return 1;
        }
    }
    return 0;
}

int main(void) {
    /* Counterpart semantics: commit the snapshot (memcpy); later local changes stay out of it */
    struct KV src[1], committed[1];
    int sn = 1;
    strcpy(src[0].key, "step");
    strcpy(src[0].val, "one");
    memcpy(committed, src, sizeof src); /* Commit snapshot */
    strcpy(src[0].val, "two");          /* Post-commit changes stay out of the snapshot */
    char buf[64];
    kv_get(committed, sn, "step", buf); printf("committed: %s\n", buf);
    kv_get(src, sn, "step", buf); printf("discarded: %s\n", buf);
    kv_get(committed, sn, "step", buf); printf("reopened: %s\n", buf);
    return 0;
}
Output
committed: one
discarded: two
reopened: one

C++ C++26

C++26
#include <iostream>
#include <map>
#include <string>

int main() {
    // Counterpart semantics: commit the snapshot (copy constructor); later local changes stay out of it
    std::map<std::string, std::string> src{{"step", "one"}};
    auto committed = src; // Commit snapshot
    src["step"] = "two";  // Post-commit changes stay out of the snapshot
    std::cout << "committed: " << committed["step"] << "\n";
    std::cout << "discarded: " << src["step"] << "\n";
    std::cout << "reopened: " << committed["step"] << "\n";
    return 0;
}
Output
committed: one
discarded: two
reopened: one

C# v11.0.100

v11.0.100
// Counterpart semantics: commit the snapshot; later local changes stay out of storage
var src = new Dictionary<string, string> { ["step"] = "one" };
var committed = new Dictionary<string, string>(src);
src["step"] = "two";
Console.WriteLine($"committed: {committed["step"]}");
Console.WriteLine($"discarded: {src["step"]}");
Console.WriteLine($"reopened: {committed["step"]}");
Output
committed: one
discarded: two
reopened: one

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// Counterpart semantics: commit the snapshot; later local changes stay out of storage
	src := map[string]string{"step": "one"}
	committed := make(map[string]string, len(src))
	for k, v := range src {
		committed[k] = v
	}
	src["step"] = "two"
	fmt.Println("committed:", committed["step"])
	fmt.Println("discarded:", src["step"])
	fmt.Println("reopened:", committed["step"])
}
Output
committed: one
discarded: two
reopened: one

Java v27.0.1

v27.0.1
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // Counterpart semantics: commit the snapshot; later local changes stay out of storage
        Map<String, String> src = new HashMap<>();
        src.put("step", "one");
        Map<String, String> committed = new HashMap<>(src);
        src.put("step", "two");
        System.out.println("committed: " + committed.get("step"));
        System.out.println("discarded: " + src.get("step"));
        System.out.println("reopened: " + committed.get("step"));
    }
}
Output
committed: one
discarded: two
reopened: one

JavaScript v26.13.1

v26.13.1
// Counterpart semantics: commit the snapshot; later local changes stay out of storage
const store = new Map([["step", "one"]]);
const committed = new Map(store); // Commit snapshot
store.set("step", "two");         // Post-commit changes stay out of the snapshot
console.log(`committed: ${committed.get("step")}`);
console.log(`discarded: ${store.get("step")}`);
console.log(`reopened: ${committed.get("step")}`);
Output
committed: one
discarded: two
reopened: one

PHP v8.6.0

v8.6.0
// session_write_close: data hits disk + the session write lock is released — PHP sessions hold a file lock after start,
// and not closing blocks concurrent requests of the same session (the classic root cause of one slow request freezing a whole site)
ini_set("session.save_path", sys_get_temp_dir());
session_start();
$_SESSION["step"] = "one";
echo session_status() === PHP_SESSION_ACTIVE ? "active\n" : "inactive\n";
session_write_close();
echo session_status() === PHP_SESSION_ACTIVE ? "active\n" : "closed\n";
// Writes to $_SESSION after closing are never persisted
$_SESSION["step"] = "two";
echo "storage: ", strpos((string) @file_get_contents(rtrim(sys_get_temp_dir(), "/") . "/sess_" . session_id()), "one") !== false ? "one" : "two", "\n";
Output
active
closed
storage: one

Python v3.15.2

v3.15.2
# Counterpart semantics: commit the snapshot; later local changes stay out of storage
store = {"step": "one"}
committed = dict(store)   # Commit snapshot (aligned with session_write_close's disk write)
store["step"] = "two"     # Post-commit changes stay out of the snapshot
print("committed:", committed["step"])
print("discarded:", store["step"])
print("reopened:", committed["step"])
Output
committed: one
discarded: two
reopened: one

Rust v1.99.1

v1.99.1
use std::collections::HashMap;

fn main() {
    // Counterpart semantics: commit the snapshot; later local changes stay out of storage
    let mut src = HashMap::new();
    src.insert("step", "one");
    let committed = src.clone(); // Commit snapshot
    src.insert("step", "two");   // Post-commit changes stay out of the snapshot
    println!("committed: {}", committed["step"]);
    println!("discarded: {}", src["step"]);
    println!("reopened: {}", committed["step"]);
}
Output
committed: one
discarded: two
reopened: one

TypeScript v26.13.1

v26.13.1
// Counterpart semantics: commit the snapshot; later local changes stay out of storage
const store = new Map<string, string>([["step", "one"]]);
const committed = new Map(store); // Commit snapshot
store.set("step", "two");         // Post-commit changes stay out of the snapshot
console.log(`committed: ${committed.get("step")}`);
console.log(`discarded: ${store.get("step")}`);
console.log(`reopened: ${committed.get("step")}`);
Output
committed: one
discarded: two
reopened: one

Language differences

C: no session-lock concept — snapshot semantics expressed as a whole-struct memcpy; a real session needs a hand-built file lock (flock) plus storage.

C++: same as C; copy construction is the snapshot.

C#: Dictionary copy construction is the snapshot; ASP.NET Session commits automatically at request end — no explicit close.

Go: hand-copy the map (no built-in map copy); gorilla/sessions auto-Saves at request end, no manual close.

Java: new HashMap<>(src) is the snapshot; Servlet Sessions are written back by the container at response end.

JavaScript: new Map(store) is the snapshot; express-session auto-saves at response end.

PHP: session_write_close does two things — persists the data and releases the file write lock; an unheld-then-released lock matters because PHP holds it from session_start until close, blocking the next request of the same session (the classic FPM one-slow-request-blocks-the-user problem); writes to $_SESSION after close are silently dropped; the script end auto-commits too, but closing early is the concurrency-friendly habit.

Python: Flask sessions likewise save at response end; at the language level the commit semantics are expressed with a dict snapshot.

Rust: clone is the snapshot; framework sessions are committed by middleware automatically.

TypeScript: same as JavaScript.