Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not launch a different domain tab from CLI #3907

Closed
junkblocker opened this issue Jun 28, 2023 · 10 comments
Closed

Can not launch a different domain tab from CLI #3907

junkblocker opened this issue Jun 28, 2023 · 10 comments
Labels
bug Something isn't working fixed-in-nightly This is (or is assumed to be) fixed in the nightly builds.

Comments

@junkblocker
Copy link
Contributor

junkblocker commented Jun 28, 2023

What Operating System(s) are you seeing this problem on?

Linux Wayland

Which Wayland compositor or X11 Window manager(s) are you using?

Whatever is the default on KDE/Plasma Fedora 38

WezTerm version

20230530-062729-95e44f21

Did you try the latest nightly build to see if the issue is better (or worse!) than your current version?

Yes, and I updated the version box above to show the version of the nightly that I tried

Describe the bug

From the matrix room discussion of Jun 2, 2023:

I use the unix_domains by default. When trying to launch a memory hungry program, I am trying to launch it from one of the unix_domains windows as a separate systemd scoped tab using the example code from https://wezfurlong.org/wezterm/config/lua/ExecDomain.html#example-running-commands-in-their-own-systemd-scope .

If I use it via the launch menu mechanism using

config.launch_menu = {
{
label = "vim (systemd scoped)",
args = { "vim" },
domain = { DomainName = 'scoped' },
},
}

it works just fine. I get a new tab running in scoped domain in the same wezterm GUI window.

When I try to use the CLI instead

wezterm cli spawn --domain-name scoped vim

it fails with

114:31:45.755  ERROR  wezterm > unexpected response Ok(ErrorResponse(ErrorResponse { reason: "Error: resolve_spawn_tab_domain: domain name scoped is invalid" })); terminating

To Reproduce

  1. Use the configuration provided.
  2. pkill -9 wezterm\*
  3. wezterm
  4. Try launching it from the wezterm launch menu. Observer that it works.
  5. Try launching it from the CLI. wezterm cli spawn --domain-name scoped vim. Observer that it does not work and throws an error.

Configuration

local wezterm = require "wezterm"
local config = wezterm.config_builder() -- {}
local callback = wezterm.action_callback
local home = wezterm.home_dir
local mux = wezterm.mux

local os = require "os"
local io = require "io"
local math = require "math"
math.randomseed(os.time())

function log(msg) wezterm.log_info(msg) end

local user = os.getenv("USER")

if true then
    function basename(str)
        if not str then
            return str
        end
        return string.gsub(str, "(.*[/\\])(.*)", "%2")
    end
    local charset = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"
    function random_string(length)
        local ret = {}
        local r
        for _ = 1, length do
            r = math.random(1, #charset)
            table.insert(ret, charset:sub(r, r))
        end
        return table.concat(ret)
    end
    config.default_domain = 'unix'
    config.unix_domains = {
        {
            name = 'unix',
        },
    }
    function wrap_in_systemd_scope(cmd)
        wezterm.log_info(cmd)

        local env = cmd.set_environment_variables
        local ident = ''
        if env.WEZTERM_UNIX_SOCKET then
            ident = "wezterm-pane-"
            .. env.WEZTERM_PANE
            .. "-on-"
            .. basename(env.WEZTERM_UNIX_SOCKET)
        else
            ident = "wezterm-pane-"
            .. env.WEZTERM_PANE
            .. "-"
            .. random_string(10)
        end

        local wrapped = {
            '/usr/bin/systemd-run',
            '--user',
            '--scope',
            '--description=Shell started by wezterm',
            '--same-dir',
            '--collect',
            '--unit=' .. ident,
        }

        for _, arg in ipairs(cmd.args or { os.getenv 'SHELL' }) do
            table.insert(wrapped, arg)
        end

        cmd.args = wrapped

        return cmd
    end
    config.exec_domains = {
        wezterm.exec_domain('scoped', wrap_in_systemd_scope),
    }
    config.launch_menu = {
       {
            label = "vim (systemd scoped)",
            args = { "vim" },
            domain = { DomainName = 'scoped' },
        },
    }
    return config
end

Expected Behavior

It should work from the CLI just like it works from the launch menu.

Logs

There is nothing in the logs around this.

Anything else?

No response

@junkblocker junkblocker added the bug Something isn't working label Jun 28, 2023
@junkblocker junkblocker changed the title Can not launch a different domained tab from CLI Can not launch a different domain tab from CLI Jun 28, 2023
wez added a commit that referenced this issue Jul 11, 2023
wez added a commit that referenced this issue Jul 11, 2023
Relocate the helper function to mux-server-impl and have both the GUI
and the mux server call it at the appropriate times.

Introduce default_mux_server_domain which is used instead of
default_domain in the mux server.  This is to avoid recursive
cycles when starting up the mux; we don't want the default
domain to be a unix client that connects to our selves because
we'll try to connect to ourselves, then in act of handling that
spawn in the default domain and try to connect to ourselves and
repeat.

refs: #3907
@wez wez added the fixed-in-nightly This is (or is assumed to be) fixed in the nightly builds. label Jul 11, 2023
@wez
Copy link
Owner

wez commented Jul 11, 2023

This should be resolved now in main.

It typically takes about an hour before commits are available as nightly builds for all platforms. Linux builds are the fastest to build and are often available within about 20 minutes. Windows and macOS builds take a bit longer.

Please take a few moments to try out the fix and let me know how that works out. You can find the nightly downloads for your system in the wezterm installation docs.

If you prefer to use packages provided by your distribution or package manager of choice and don't want to replace that with a nightly download, keep in mind that you can download portable packages (eg: a .dmg file on macOS, a .zip file on Windows and an .AppImage file on Linux) that can be run without permanently installing or replacing an existing package, and can then simply be deleted once you no longer need them.

If you are eager and can build from source then you may be able to try this out more quickly.

@junkblocker
Copy link
Contributor Author

junkblocker commented Jul 11, 2023

  1. Seems to launch correctly.
  2. If I have this update-status handler and I launch nvim with wezterm cli spawn --domain-name scoped -- nvim and switch to that tab, the left status still shows unix:default.
    wezterm.on("update-status", function(window, pane)
        local status_str = ""

        local ok, domain_name = pcall(function()
            return pane:get_domain_name()
        end)
        if not ok then
            log("not ok")
        else
            log("domain_name: " .. domain_name)
        end
        log("active_workspace: " .. window:active_workspace())
        if ok and domain_name then
            status_str = pane:get_domain_name() .. ":" .. window:active_workspace()
        end
        log("active_workspace: " .. window:active_workspace())
        log("status_str: " .. status_str)
        window:set_left_status(status_str)
    end)

rather than scoped:default that I am expecting. Is that a bug or is my understanding incorrect?

@wez
Copy link
Owner

wez commented Jul 11, 2023

Your gui instance is using its unix domain client to connect to the mux server, which is in turn using the scoped domain.
The domains on the "remote" server are not visible to the GUI client, which only knows about its unix connection to that server.

@junkblocker
Copy link
Contributor Author

Ok. I'll probably need to find a solution to displaying the remote domain name maybe in a discussion on matrix chat. But the fix works.

Tested with

  1. tail /dev/zero - without systemd scoped domain gets the whole wezterm gui + mux server OOM killed.
  2. wezterm cli spawn --domain-name scoped -- tail /dev/zero just gets that new tab OOM killed.

Thanks for the fix! This bug can be closed now.

@junkblocker
Copy link
Contributor Author

junkblocker commented Jul 11, 2023

I am observing some strange interaction with a json I store/load into a global variable that has nothing to do with domains. If I load it, it makes the scoped domain disappear and throw this (redacted) error.

12:32:10.079  ERROR  wezterm > unexpected response Ok(ErrorResponse(ErrorResponse { reason: "Error: resolve_spawn_tab_domain: domain name \"scoped\" is invalid. Possible names are \"SSHMUX:work\", \"SSHMUX:bazaar.launchpad.net\",, \"SSH:router\", \"SSHMUX:ssh.github.com\", , \"unix\", \"SSHMUX:github.com\",  \"SSH:gist.github.com\", \"SSH:ssh.github.com\", \"SSH:bitbucket.org\", \"SSHMUX:gitlab.com\", \"SSHMUX:gist.github.com\", \"SSHMUX:bitbucket.org\",  \"SSHMUX:router\", \"SSH:github.com\", \"SSH:bazaar.launchpad.net\", \"local\",  \"SSH:gitlab.com\"." })); terminating    

The code is doing this

-- Load JSON data from a file
function load_json(file)
    local ok, f = pcall(io.open, file, "r")
    if not ok then
        return nil, nil
    end
    if not f then
        return nil, nil
    end
    local text = f:read "*a"
    f:close()
    local ok, val = pcall(wezterm.json_parse, text)
    if not ok then
        print "Error parsing json"
        print(val)
    end
    return ok, val
end

    if wezterm.GLOBAL.WALLPAPER == nil then
        local ok, val = load_json(WALLPAPER_SETTINGS_DB) -- json file path
        if ok and type(val) == "table" then
            -- if this branch is taken the bug appears
            wezterm.GLOBAL.WALLPAPER = val -- utils.table.deepclone(val) does not help either
        else
            -- if this branch is taken then everything is fine
            wezterm.GLOBAL.WALLPAPER = {}
        end
    end

The loaded json has this structure

{
  "DEFAULTS": [
    "/home/xxx/Pictures/Wallpapers/myfav-2560x1440.jpg",
    "/home/xx/Pictures/Wallpapers/alsomyfav-5196x3056.jpg",
  ],
  "DIR": "/home/xxx/Pictures/Wallpapers",
  "DISABLE_UNTIL": 0,
  "ENABLE": false,
  "SETTINGS": {}
}

@wez
Copy link
Owner

wez commented Jul 11, 2023

most likely there is a lua syntax error and your config file is not being parsed by the mux server

@junkblocker
Copy link
Contributor Author

Ah, thanks for that hint. I had an overloading of config as a variable. Fixing that fixed it.

@junkblocker
Copy link
Contributor Author

junkblocker commented Jul 11, 2023

Closed too soon. Still investigating that.

@junkblocker
Copy link
Contributor Author

Thanks. Everything works now!

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working fixed-in-nightly This is (or is assumed to be) fixed in the nightly builds.
Projects
None yet
Development

No branches or pull requests

2 participants