LPC callback that returns a reference produces a future containing a dangling reference

Issue #413 resolved
Amir Kamil created an issue

Test code:

#include <upcxx/upcxx.hpp>
#include "../util.hpp"

struct T {
  bool valid = true;
  ~T() { valid = false; }
};

T global;

int main() {
  upcxx::init();
  print_test_header();

  upcxx::persona &target = upcxx::current_persona();
  upcxx::future<T const&> f =
    target.lpc([]() -> T const & { return global; });
  UPCXX_ASSERT_ALWAYS(f.wait_reference().valid);

  print_test_success();
  upcxx::finalize();
}

Result:

Test: issue413.cpp
Ranks: 1
*** FATAL ERROR (proc 0): 
//////////////////////////////////////////////////////////////////////
UPC++ assertion failure:
 on process 0 (marauder.local)
 at test/regression/issue413.cpp:18
 in function: int main()

Failed condition: f.wait_reference().valid

To have UPC++ freeze during these errors so you can attach a debugger,
rerun the program with GASNET_FREEZE_ON_ERROR=1 in the environment.
//////////////////////////////////////////////////////////////////////

NOTICE: Before reporting bugs, run with GASNET_BACKTRACE=1 in the environment to generate a backtrace. 
*** Caught a fatal signal (proc 0): SIGABRT(6)
Abort trap: 6

Some relevant discussion in PR #272.

Comments (5)

  1. Amir Kamil reporter

    Possible fix, if we decide that the reference is preserved:

    diff --git a/src/persona.hpp b/src/persona.hpp
    index 49efb33e..6264d508 100644
    --- a/src/persona.hpp
    +++ b/src/persona.hpp
    @@ -105,7 +105,11 @@ namespace upcxx {
    
           template<typename ...Args>
           void operator()(Args &&...args) {
    -        std::tuple<typename std::decay<Args>::type...> results{
    +        std::tuple<typename std::conditional<
    +            std::is_rvalue_reference<Args>::value,
    +            typename std::decay<Args>::type,
    +            Args
    +          >::type...> results{
               std::forward<Args>(args)...
             };
    

  2. Dan Bonachea

    For the record, this issue is also responsible for compile errors seen on lpc-ctor-trace with Apple XCode up to Apple LLVM version 9.0.0 : nightly CI

  3. Log in to comment