fukamachi / lack

Lack, the core of Clack
MIT License
156 stars 33 forks source link

GENERATE-RANDOM-ID: The value -1 is not of type (UNSIGNED-BYTE 64) #81

Closed daninus14 closed 10 months ago

daninus14 commented 10 months ago

I am getting the following error:

The value -1 is not of type (UNSIGNED-BYTE 64)

Here's the stacktrace:

Backtrace for: #<SB-THREAD:THREAD "hunchentoot-worker-127.0.0.1:58169" RUNNING {1001172B03}>
0: (CL-ISAAC:RAND64 #S(CL-ISAAC:ISAAC64-CTX :RANDCNT 0 :RANDRSL #(3925593177097861953 9711298613570277120 13301789079780403768 5851447409938752469 7395316721195156215 3876317782763891039 17324142642302461745 5173381862221173775 2232769689812699852 641431748002290388 7852246810531221939 12946534270186310260 ...) :RANDMEM #(15932170542592125262 5641653233185709706 17469926063756623801 18436779483168812525 6140313674221961030 10760809802976968553 8867473602531037886 4928843196377889572 17910188103990029047 6963694444940152626 13295689543317008908 3498414824101057605 ...) :A 14509949935736105173 :B 10253657088000355703 :C 1025))
1: (CL-ISAAC:RAND-BITS-64 #S(CL-ISAAC:ISAAC64-CTX :RANDCNT 0 :RANDRSL #(3925593177097861953 9711298613570277120 13301789079780403768 5851447409938752469 7395316721195156215 3876317782763891039 17324142642302461745 5173381862221173775 2232769689812699852 641431748002290388 7852246810531221939 12946534270186310260 ...) :RANDMEM #(15932170542592125262 5641653233185709706 17469926063756623801 18436779483168812525 6140313674221961030 10760809802976968553 8867473602531037886 4928843196377889572 17910188103990029047 6963694444940152626 13295689543317008908 3498414824101057605 ...) :A 14509949935736105173 :B 10253657088000355703 :C 1025) 160)
2: (LACK.UTIL:GENERATE-RANDOM-ID)

I wasn't getting the error before. Not sure if it's because of an update.

Any ideas on what I can do?

daninus14 commented 10 months ago

It looks like this is the culprit: GENERATE-RANDOM-ID

daninus14 commented 10 months ago

Update: I think this is a problem with the seed and the underlying library used for generating the random numbers.

To reproduce the problem:

(let ((ctx (isaac:init-self-seed :count 5 :is64 T)))
  (dotimes (n 100) (isaac:rand-bits-64 ctx 160)))

So since lack.util is using isaac, it's causing a problem after a number of random numbers generated.

Doing this:

(let ((ctx (isaac:init-self-seed :count 5 :is64 T)))
  (dotimes (n 100)
    (format T "~A: ~A~%" n (isaac:rand-bits-64 ctx 160))))

I seem to consistently get an issue after 84 runs.

I will check the cl-isaac docs, however, this is an issue in ningle itself for relying on this library with an issue.

Can you please fix this?

By the way, is this thread safe? Since ningle is using threads, we should be concerned as well. I haven't tested for issues with thread safety, but another thing to think about.

What are your thoughts?

svetlyak40wt commented 10 months ago

I've figured out that cl-isaac's context is not thread-safe. Thus generate-random-id should wrap the call to cl-isaac into the bt2:with-lock.

daninus14 commented 10 months ago

@svetlyak40wt thanks for checking that!

Update: @svetlyak40wt also fixed the underlying bug in the cl-isaac library, however, it looks like the maintainer is not active in github so he hasn't accepted the pull-request. Because of that, unless one is using qlot and specifying the fork, or downloading to local-projects in quicklisp, this project (lack) is broken for depending on cl-isaac.

Summary of What Needs To Get Done:

In the meantime I'll send an email to @thephoeron to see if he can accept the PR.

daninus14 commented 10 months ago

Closing this since PR #82 has been merged. Thanks