Skip to content

cbindgen panicks when parse-expand is run inside of buildscript due to inability to acquire semaphore #347

@Wodann

Description

@Wodann

I have successfully run cbingen on the command-line using the following cbindgen.toml:

include_guard = "SE4_CORE_BINDINGS_H_"
include_version = true
braces = "SameLine"
line_length = 100
tab_width = 4

[export]
prefix = "SE4"

[parse]
parse_deps = false

[parse.expand]
crates = ["se4_capi"]

I decided to add it as a build script:

extern crate cbindgen;

use std::env;

fn main() {
    let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

    cbindgen::generate(crate_dir)
        .expect("Unable to generate bindings")
        .write_to_file("include/bindings.h");
}

Upon execution, of cargo build cbindgen instantly crashes. It seems like cbindgen tries to acquire a lock on the build directory that was previously acquired by cargo, and thus crashes after outputing the following:

    Blocking waiting for file lock on build directory
   Compiling se4_capi v0.1.0
error: failed to run custom build command for `se4_capi v0.1.0`

Caused by:
  process didn't exit successfully: `build-script-build` (exit code: 101)
--- stderr
thread 'main' panicked at 'Unable to generate bindings: CargoExpand("se4_capi", Compile("    Blocking waiting for file lock on build directory\nerror: Too many posts were made to a semaphore. (os error 298)\n"))', src\libcore\result.rs:999:5
stack backtrace:
   0: std::sys::windows::backtrace::set_frames
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\sys\windows\backtrace\mod.rs:95
   1: std::sys::windows::backtrace::unwind_backtrace
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\sys\windows\backtrace\mod.rs:82
   2: std::sys_common::backtrace::_print
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\sys_common\backtrace.rs:71
   3: std::sys_common::backtrace::print
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\sys_common\backtrace.rs:59
   4: std::panicking::default_hook::{{closure}}
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panicking.rs:197
   5: std::panicking::default_hook
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panicking.rs:211
   6: std::panicking::rust_panic_with_hook
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panicking.rs:474
   7: std::panicking::continue_panic_fmt
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panicking.rs:381
   8: std::panicking::rust_begin_panic
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panicking.rs:308
   9: core::panicking::panic_fmt
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libcore\panicking.rs:85
  10: core::result::unwrap_failed<cbindgen::bindgen::error::Error>
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\src\libcore\macros.rs:18
  11: core::result::Result<cbindgen::bindgen::bindings::Bindings, cbindgen::bindgen::error::Error>::expect<cbindgen::bindgen::bindings::Bindings,cbindgen::bindgen::error::Error>
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\src\libcore\result.rs:827
  12: build_script_build::main
             at .\build.rs:9
  13: std::rt::lang_start::{{closure}}<()>
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\src\libstd\rt.rs:64
  14: std::rt::lang_start_internal::{{closure}}
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\rt.rs:49
  15: std::panicking::try::do_call<closure,i32>
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panicking.rs:293
  16: panic_unwind::__rust_maybe_catch_panic
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libpanic_unwind\lib.rs:85
  17: std::panicking::try
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panicking.rs:272
  18: std::panic::catch_unwind
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\panic.rs:388
  19: std::rt::lang_start_internal
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\/src\libstd\rt.rs:48
  20: std::rt::lang_start<()>
             at /rustc/37ff5d388f8c004ca248adb635f1cc84d347eda0\src\libstd\rt.rs:64
  21: main
  22: invoke_main
             at d:\agent\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  23: __scrt_common_main_seh
             at d:\agent\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  24: BaseThreadInitThunk
  25: RtlUserThreadStart

I am aware of PR #72, and as such attempted to set CARGO_EXPAND_TARGET_DIR, but the only change was that it took a little longer for the panick to occur.

env::set_var("CARGO_EXPAND_TARGET_DIR", crate_dir.clone());

I also read PR #153 which seems to have a similar problem, except that it doesn't result in a panick.

To use cbindgen I swapped to nightly, as it was required for expanding macros. I am currently at:

nightly-x86_64-pc-windows-msvc (default)
rustc 1.36.0-nightly (37ff5d388 2019-05-22)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions