#What seems like a Torn Read

28 messages · Page 1 of 1 (latest)

split drift
#

so we have this function, which is invoked with go so each invocation should be independent, my problem is that when calculating the offset value, which is uint64 derived from a bunch of uint64 timestamps, I get extremely large values, like 9223372036852854358 which is suspiciously close to the half of the uint64 max value, which makes me think it's a torn read issue

logging the timestamp values (t1,t2 etc) and trying to do the calculations myself does not really compute, I've also tried to running the calculation multiple times in a row and it still gets the same value

func synchronizeWithPeer(target string, lrpcGW *lrpc.Gateway) {
    fmt.Printf("starting peer synchronization with \"%s\"\n", target)
    probesPacket, err := lrpcGW.RequestWithResponse(variables.GetSensorAddress(), "START_PROBE", []byte(target))
    if err != nil {
        fmt.Println("gettings probes failed", err)
        return
    }
    var probes map[string]uint64
    err = json.Unmarshal(probesPacket.Payload, &probes)
    if err != nil {
        fmt.Println("failed to parse probes", err)
        return
    }
    t1, t2, r1, r2, pt1, pt2 := probes["t1"], probes["t2"], probes["r1"], probes["r2"], probes["pt1"], probes["pt2"]

    if !validCodedProbes(t1, t2, r1, r2) {
        fmt.Println("invalid probe data")
        return
    }

    newOffSet := getClockOffset(t1, t2, r1, r2, pt1, pt2)
        fmt.Printf("new offset: %d\n", newOffSet)
    if newOffSet > 10000000 {
        fmt.Println(t1, t2, r1, r2, pt1, pt2)
    }

    
}

func validCodedProbes(t1, t2, r1, r2 uint64) bool {
    return (r2-r1)-(t2-t1) < 50_000_000 && r2 > r1
}

func getClockOffset(t1, t2, r1, r2, pt1, pt2 uint64) int64 {
    o1 := ((pt1 - t1) + (pt1 - r1)) / 2
    o2 := ((pt2 - t2) + (pt2 - r2)) / 2
    fmt.Println("o1", o1)
    fmt.Println("o2", o2)
    return int64(float64(o1+o2) / 2)
}

an example of the logged values

1713218130973383900 1713218131028881800 1713218131185150200 1713218131258211300 1713218131077345600 1713218131147361300
o1 9223372036852854358
o2 3814750
new offset: 4611686018428334080
#

I have a feeling the logged timestamp values are correct at the time of logging them but not at the time of calculations, but not sure how to solve that

#

I guess a simple sanity check is to put a mutex lock and see if the problem goes away

frank holly
#

@split drift are you sure the issue isn't the conversion of o1+o2 to a float

split drift
#

i added that to try and fix the issue

#

including the int64 conversion too

frank holly
#

its overflowing when you convert it to int64

gilded eagle
#

i don't see any overflow on the final conversion, the new offset is correct based on the logged values of o1 and o2

#

there certainly could be earlier, though

frank holly
#

you're right

#

order of operations

split drift
#

huh, could it be correct and just overflowing

#

guess I'll need to shave off the first digit since I dont need the year in my calculations

unkempt seal
#

you can avoid overflowing by: o1 + (o2 - o1) / 2

#

o1 and o2 too can overflow, you need to be careful

split drift
#

hmm, I've removed the first 3 digits, so that should've given me a lot of free room but it's still overflowing

#

I really dont get it, i've made the timestamps smaller, removed the division that could've been causing issues

func getClockOffset(t1, t2, r1, r2, pt1, pt2 uint64) uint64 {
    o1 := ((pt1 - t1) + (pt1 - r1))
    o2 := ((pt2 - t2) + (pt2 - r2))
    fmt.Println("o1", o1)
    fmt.Println("o2", o2)
    return (o1 + o2)
}

and im getting these results

13220517847940700 13220517898854900 13220518064293600 13220518131099700 13220517952683700 13220518014084200
o1 18446744073702684716
o2 18446744073707765416
new offset: 18446744073700898516

#

running those should've gotten me
o2 = ((pt2 - t2) + (pt2 - r2)) / 2
-779400
o1 =((pt1 - t1) + (pt1 - r1)) / 2
1795850

gilded eagle
#

do you notice anything interesting about one of those numbers

#

especially given that you're using unsigned integers here

#

😛

split drift
#

that it's the max value for the uint64?

gilded eagle
#

-779400

split drift
#

im going to go

#

delete myself

#

thanks a lot for the help