Importing CairoMakie prevents sampling

I have a script that runs fine unless I have import CairoMakie at the top. Then it hangs. How can I diagnose this?

It hangs at mcmc_with_warmup. The same thing happens whether I have using CairoMakie or import CairoMakie. However, if I run the script before importing CairoMakie, then import it and run the script again, it works fine.

It seems like it’s hanging before it starts executing the line.

using AbstractGPs
using Distributions
using StatsFuns
using DynamicHMC
using LogDensityProblems
# import CairoMakie
using Random
Random.seed!(1234)


n = 100
x_train = collect(range(-5.0, 5.0; length=n))
y_train = rand(n) + sin.(x_train .* 2);


function gp_loglikelihood(x, y)
    function loglikelihood(params)
        kernel =
            softplus(params[1]) * (Matern52Kernel() ∘ ScaleTransform(softplus(params[2])))
        f = GP(kernel)
        fx = f(x, 0.1)
        return logpdf(fx, y)
    end
    return loglikelihood
end

function gp_posterior(x, y, p)
    kernel = softplus(p[1]) * (Matern52Kernel() ∘ ScaleTransform(softplus(p[2])))
    f = GP(kernel)
    return posterior(f(x, 0.1), y)
end


loglik_train = gp_loglikelihood(x_train, y_train)
logprior(params) = logpdf(MvNormal(2, 1), params)

n_samples = 2_000
n_adapts = 1_000


# Log joint density
function LogDensityProblems.logdensity(ℓ::typeof(loglik_train), params)
    return ℓ(params) + logprior(params)
end

# The parameter space is two-dimensional
LogDensityProblems.dimension(::typeof(loglik_train)) = 2

# `loglik_train` does not allow to evaluate derivatives of
# the log-likelihood function
function LogDensityProblems.capabilities(::Type{<:typeof(loglik_train)})
    return LogDensityProblems.LogDensityOrder{0}()
end


mcmc_result = mcmc_with_warmup(
        Random.GLOBAL_RNG,
        ADgradient(:ForwardDiff, loglik_train),
        n_samples;
        reporter=NoProgressReport(),
)

samples = mcmc_result.chain


samples_constrained = [map(softplus, p) for p in samples]
mean_samples = mean(samples_constrained)

Julia 1.5.3
Linux

Does using CairoMakie` hang when it is the only line? What happens when you interrupt it with Ctrl-C?

using CairoMakie by itself works fine. The part that hangs is the mcmc_with_warmup line. Ctrl+C doesn’t do anything. I have to Ctrl+\ to get out.

Could you try this with just Cairo.jl? If it still hangs that would point to something outside of our control.

Works fine with just import Cairo.

It seems CairoMakie prevents the code from running, when Cairo alone doesn’t cause problems. I can import CairoMakie after doing the computation, but that’s a hack.

@sdanisch any idea what this could be? Cairomakie doesn’t have a renderloop right, and I don’t see what could be happening there in the background

I can’t reproduce it on windows & julia 1.6…