Brave cannot be executed (exit code 141) from Obsidian

Description of the issue: I’ve set my default browser to be Obsidian, and confirmed that from the command line execute xdg-open "http://community.brave.com/" will launch Brave. However, if I run Obsidian (an app you can download from https://obsidian.md/ ) and click on a link in my Obsidian document, the link is opened in Firefox instead of Brave. This is because Brave seems to be crashing with exit code 141 when executed via Obsidian → xdg-open → Brave

How can this issue be reproduced?

  1. Run Obsidian.
  2. Write a document with a link on it.
  3. Ctrl-click on the link.

Expected result: The link will be opened in Brave.

Brave Version( check About Brave): Version 1.46.144 Chromium: 108.0.5359.128 (Official Build) (64-bit)

Additional Information:

Obsidian, like most Linux applications, uses xdg-open to decide how to open a link.

In the source code for xdg-open, there’s a function search_desktop_file which searches through all the .desktop files, and tries to execute the relevant one

https://cgit.freedesktop.org/xdg/xdg-utils/tree/scripts/xdg-open.in?id=8ae02631a9806da11b34cd6b274af02d28aee5da#n268

Once xdg-open finds the right desktop file, it does this:

    env "$command" "$@"

    if [ $? -eq 0 ]; then
        exit_success
    fi

Where I’ve confirmed that $command is brave and $@ is the URL to open.

When I run xdg-open https://example.com/ from the command line, the exit code is 0, and so xdg-open is satisfied and stops here, performing the exit_success function.

But when I click on a link in Obsidian, the exit code is 141, and so xdg-open doesn’t exit, and proceeds to execute some fallback logic, which eventually leads to running Firefox.

It’s unclear to me why Brave is using exit code 0 in one case and exit code 141 in the other.

Nevermind, you can close this issue. I found out that this is due to a specific quirk of my setup.

I’ve set up some systemd-slice https://www.freedesktop.org/software/systemd/man/systemd.slice.html to limit the memory usage of individual Brave processes and I wrote a wrapper script in Ruby that would try to detect whether an existing instance of Brave was running, and either redirect URLs to the existing instance, or launch a new instance in the slice:

#!/usr/bin/env ruby

BRAVE_LOCATION="/usr/bin/brave"

if BRAVE_LOCATION.empty? # TODO: Check if file exists and is executable.
  puts "Could not find brave."
  exit 1
end

system('pgrep -x brave > /dev/null')
if $?.exitstatus == 0
  puts "Found existing brave instance; redirecting request to that instance..."
  Process.detach(fork do
    [$stdout, $stderr].each { |s|
      s.reopen(File.new('/dev/null', 'w'))
    }
    exec(BRAVE_LOCATION, *ARGV)
  end)
else
  puts "Starting new instance of brave in web-browsers.slice cgroup..."
  Process.detach(fork do
    [$stdout, $stderr].each { |s|
      s.reopen(File.new('/dev/null', 'w'))
    }
    exec("systemd-run", "--user", "--slice=web-browsers.slice", "--working-directory=#{Dir.getwd}", BRAVE_LOCATION, *ARGV)
  end)
end

This is likely what was returning the error code 141. When I rewrote the script in bash, I consistently get an exit code of 0 and when I ctrl-click on links in Obsidian, the links correctly open in Brave.

#!/usr/bin/env bash

BRAVE_LOCATION="/usr/bin/brave"

if [ ! -f "$BRAVE_LOCATION" ]; then
	echo "Could not find brave at $BRAVE_LOCATION"
	exit 1
fi

pgrep -x brave > /dev/null

if [ $? -eq 0 ]; then
	echo "Found existing brave instance; redirecting request to that instance..."
	exec $BRAVE_LOCATION $@
else
	echo "Starting new instance of brave in web-browsers.slice cgroup..."
	WORKING_DIR=$(pwd)
	exec systemd-run --user --slice=web-browsers.slice --working-directory="$WORKING_DIR" $BRAVE_LOCATION $@
fi

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.