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

DCLI is broken for Dart 3.5 #252

Closed
jifferon opened this issue Aug 13, 2024 · 13 comments
Closed

DCLI is broken for Dart 3.5 #252

jifferon opened this issue Aug 13, 2024 · 13 comments

Comments

@jifferon
Copy link

jifferon commented Aug 13, 2024

Hi! Unfortunately, DCLI 6.0.1 (and 6.0.0) is broken on Dart 3.5

When I do dcli compile --install

This is the error log:

ArgumentError: Invalid argument(s): Couldn't resolve native function 'pthread_mutex_timedlock' in 'package:native_synchronization_temp/src/bindings/pthread.dart' : No asset with id 'package:native_synchronization_temp/src/bindings/pthread.dart' found. No available native assets. Attempted to fallback to process lookup. dlsym(RTLD_DEFAULT, pthread_mutex_timedlock): symbol not found.


Stacktrace: dart:ffi                                                            Native._ffi_resolver_function
package:native_synchronization_temp/src/bindings/pthread.dart       pthread_mutex_timedlock
package:native_synchronization_temp/posix.dart 58:20                _PosixMutex._timedLock
package:native_synchronization_temp/posix.dart 44:7                 
_PosixMutex._lock
package:native_synchronization_temp/primitives.dart 75:5            
Mutex.runLocked
package:native_synchronization_temp/mailbox.dart 109:19             Mailbox._takeTimed
package:native_synchronization_temp/mailbox.dart 100:14             
Mailbox.take
package:dcli/src/util/runnable_process.dart 341:38                  RunnableProcess.start
package:dcli/src/util/runnable_process.dart 194:7                   RunnableProcess.run
package:dcli/src/functions/run.dart 180:19                          
startFromArgs


ERROR: package:dcli/src/script/dart_sdk.dart 247:7                         DartSdk.runPub
package:dcli/src/script/dart_sdk.dart 381:5                         
DartSdk.runPubGet
package:dcli/src/script/pub_get.dart 31:17                          
PubGet.run
package:dcli/src/script/dart_project.dart 354:14                    DartProject._pubget.<fn>
package:dcli/src/util/named_lock.dart 125:17                        NamedLock.withLockAsync
package:dcli/src/script/dart_project.dart 346:38                    DartProject._pubget
package:dcli/src/script/dart_project.dart 257:21                    DartProject.warmup.<fn>
package:dcli/src/util/named_lock.dart 125:17                        NamedLock.withLockAsync
package:dcli/src/script/dart_project.dart 235:5                     DartProject.warmup
package:dcli_sdk/src/commands/compile.dart 109:10                   CompileCommand.compileScript
package:dcli_sdk
ERROR: /src/commands/compile.dart 178:20                   CompileCommand.compileScripts
package:dcli_sdk/src/commands/compile.dart 65:7                     CompileCommand.run
package:dcli_sdk/src/script/command_line_runner.dart 101:11         CommandLineRunner.process
package:dcli_sdk/src/script/entry_point.dart 43:18                  EntryPoint._parseCmdLine
../../.pub-cache/hosted/pub.dev/dcli_sdk-6.0.0/bin/dcli.dart 17:22  DCli.run
../../.pub-cache/hosted/pub.dev/dcli_sdk-6.0.0/bin/dcli.dart 12:3   main

dcli doctor is:

DCli version      6.0.1                                                  

os                macos                                                  
os version        Version 14.5 (Build 23F79)
path separator    /                                                      

dart version      3.5.0                                                  

dcli path         /<HOME>/.pub-cache/bin/dcli                            
dart exe path     /<HOME>/flutter/bin/dart                               
dart path         /<HOME>/flutter/bin/dart                                which: /<HOME>/flutter/bin/dart                        
compiler          using 'dart compile exe'                               

pub               using 'dart pub'                                       

pub cache         /<HOME>/.pub-cache                                     
PUB_CACHE Env     false                                                  

package config    /<HOME>/not passed                                     
@bsutton
Copy link
Collaborator

bsutton commented Aug 13, 2024 via email

@dw2kim
Copy link

dw2kim commented Aug 13, 2024

+1

@bsutton
Copy link
Collaborator

bsutton commented Aug 14, 2024

I've published dcli_sdk 6.0.2 which should resolve this issue.

@bsutton bsutton closed this as completed Aug 14, 2024
@danieletulone
Copy link

danieletulone commented Aug 18, 2024

Hi, I have same problem.
dcli version is 6.0.5 and dart 3.4.3, on macos arm64

ArgumentError: Invalid argument(s): Couldn't resolve native function 'pthread_mutex_timedlock' in 'package:native_synchronization_temp/src/bindings/pthread.dart' : No asset with id 'package:native_synchronization_temp/src/bindings/pthread.dart' found. No available native assets. Attempted to fallback to process lookup. dlsym(RTLD_DEFAULT, pthread_mutex_timedlock): symbol not found.

@bsutton
Copy link
Collaborator

bsutton commented Aug 18, 2024 via email

@danieletulone
Copy link

@bsutton Yes, I updated to 3.5 (3.5.1), the error still remains.

@bsutton
Copy link
Collaborator

bsutton commented Aug 18, 2024 via email

@danieletulone
Copy link

danieletulone commented Aug 19, 2024

@bsutton The solution seems to be to provide a custom implementation of the pthread_mutex_timedlock function.
I'm making some tries editing directly the pub content.

I don't known low level details so with the help of chatgpt I replaced pthread_mutex_timedlock function in native_synchronization_temp/lib/src/bindings/pthread.dart with:

/// at the top, add another import
import 'package:ffi/ffi.dart';

/// Constants (maybe we should get these values in another way, currently hard coded)
const int EBUSY = 16; // Resource busy
const int ETIMEDOUT = 60; // Operation timed out
const int CLOCK_REALTIME = 0;

/// Bind the `clock_gettime` function from the C library
final clock_gettime = DynamicLibrary.process().lookupFunction<
    Int32 Function(Int32 clockId, Pointer<pthread_timespec_t> tp),
    int Function(int clockId, Pointer<pthread_timespec_t> tp)>('clock_gettime');

/// Mutex lock implementation for macOS
int pthread_mutex_timedlock(
    Pointer<pthread_mutex_t> mutex, Pointer<pthread_timespec_t> abstime) {
  final pthreadMutexTryLock = DynamicLibrary.process().lookupFunction<
      Int Function(Pointer<pthread_mutex_t>),
      int Function(Pointer<pthread_mutex_t>)>('pthread_mutex_trylock');

  final nanosleep = DynamicLibrary.process().lookupFunction<
      Int Function(
          Pointer<pthread_timespec_t> req, Pointer<pthread_timespec_t> rem),
      int Function(Pointer<pthread_timespec_t> req,
          Pointer<pthread_timespec_t> rem)>('nanosleep');

  final timeout = abstime.ref;
  final now = malloc<pthread_timespec_t>();
  final remainingTime = malloc<pthread_timespec_t>();

  while (true) {
    final ret = pthreadMutexTryLock(mutex);
    if (ret == 0) {
      malloc.free(now);
      malloc.free(remainingTime);
      return 0;
    } else if (ret != EBUSY) {
      malloc.free(now);
      malloc.free(remainingTime);
      return ret;
    }

    clock_gettime(CLOCK_REALTIME, now);

    if (now.ref.tv_sec > timeout.tv_sec ||
        (now.ref.tv_sec == timeout.tv_sec &&
            now.ref.tv_nsec >= timeout.tv_nsec)) {
      malloc.free(now);
      malloc.free(remainingTime);
      return ETIMEDOUT;
    }

    remainingTime.ref.tv_sec = timeout.tv_sec - now.ref.tv_sec;
    remainingTime.ref.tv_nsec = timeout.tv_nsec - now.ref.tv_nsec;
    if (remainingTime.ref.tv_nsec < 0) {
      remainingTime.ref.tv_sec -= 1;
      remainingTime.ref.tv_nsec += 1000000000;
    }

    nanosleep(remainingTime, nullptr);
  }
}

Now the cli works, I have tried with a 'some command'.run (that previously didn't work)

I don't know if the provided code is a right mock implementation of the pthread_mutex_timedlock.

@bsutton
Copy link
Collaborator

bsutton commented Aug 19, 2024 via email

@bsutton bsutton reopened this Aug 19, 2024
@bsutton
Copy link
Collaborator

bsutton commented Aug 19, 2024

So I believe that what happened is:

when I released dcli 6.0.5 I didn't update the minimum constraint on the dcli library for dcli_sdk.

On a clean system dcli_sdk links to dcli 6.0.5 but I think on a system with an earlier version of dcli, that dart links the dcli_sdk to the old resident version of dcli.

So I've now update the min version for dcli in dcli_sdk which I hope resolves this issue.

I've released dcli_sdk 6.0.5 to pub.dev with the fix.

let me know how this goes.

@bsutton
Copy link
Collaborator

bsutton commented Aug 19, 2024

And now I've just read your updated comment and realised what the code was for.

So you are saying that macos is missing the pthread_mutex_timedlock. That puts quite a different shade on the problem.

So I don't have time now but I will look at the practicalities of integrating your mutex code into the library.

At a quick look I don't think the chat gpt implementation is correct.

This link proposes a solution:

https://lists.apache.org/thread/yqdnsky6svc5cfdkx1s8m4d3j8h9xyj6

edit: updated link

@danieletulone
Copy link

So, the issue is more related to native_synchronization_temp/native_synchronization?

@bsutton
Copy link
Collaborator

bsutton commented Aug 19, 2024

@danieletulone I've move this discussion to a new issue as its a separate problem.

#253

@bsutton bsutton closed this as completed Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants