From 04f8ce0edf936e53bc197ac58bb64afcfaaf2219 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Thu, 8 Aug 2024 22:57:01 -0300 Subject: Fix: wtmux: session isolation through distinct sockets --- wtmux | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/wtmux b/wtmux index b577fd8..3853282 100755 --- a/wtmux +++ b/wtmux @@ -15,19 +15,38 @@ opts="" name="$1" state="$2" +socket="-L${name}" # Session selection. if [ ! -z "${name}" ]; then - if ! tmux list-sessions 2> /dev/null | grep -q "^${name}:"; then + if [ "${name}" = "root" ]; then + sudo tmux attach + exit + fi + + if ! tmux ${socket} list-sessions 2> /dev/null | grep -q "^${name}:"; then session="new-session -d -s ${name}" # Use a unique, per-session name - #session"${session} -t ${name}" + #session="${session} -t ${name}" + + # Ensure there's a separate server running for each session. + # + # This achieves session isolation by using a unique socket per session. + # + # This solves tmux creating too many environment variables and triggering + # the following error when using programs such as "less" and "ranger": + # + # Error: too many environment variables: >= MAX_ENVS (256) + # + # For a list of tmux environment variables available within a session, + # type "env | grep TMUX". + # + # Reference: + # https://github.com/tmux/tmux/wiki/Advanced-Use + session="${socket} ${session}" - if [ "${name}" = "root" ]; then - sudo tmux attach - exit - elif [ -f "$HOME/.tmux/${name}" ]; then + if [ -f "$HOME/.tmux/${name}" ]; then opts="$HOME/.tmux/${name}" elif [ -f "$HOME/.tmux/base" ]; then opts="$HOME/.tmux/base" @@ -36,15 +55,15 @@ if [ ! -z "${name}" ]; then fi # Start session. - if [ ! -z "$opts" ]; then - tmux $session \; source-file $opts + if [ ! -z "${opts}" ]; then + tmux ${session} \; source-file ${opts} else - tmux $session + tmux ${session} fi fi if [ "$state" != "detached" ]; then - tmux attach -t ${name} + tmux ${socket} attach -t ${name} fi else tmux -- cgit v1.2.3