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

Timestamp sync seems broken #1014

Open
andresdrb opened this issue Feb 27, 2022 · 49 comments
Open

Timestamp sync seems broken #1014

andresdrb opened this issue Feb 27, 2022 · 49 comments

Comments

@andresdrb
Copy link

Describe the bug
Since upgraded to 8.x version the timestamp sync ssems to intermitently (every few hours) fail and spend 1-2 hours out of sync (givin "Timestamp for this request was 1000ms ahead..." messages).

To Reproduce
Happens on all endpoints called (I call accountInfo, exchangeInfo, trading, etc). AutoTimestamp setting is set to true

@JKorf
Copy link
Owner

JKorf commented Feb 27, 2022

Hi, are you using the latest version? There was an earlier version where the time offset would not get reset after the difference between the server and the client got smaller than 500ms after being more than 500ms before.

@andresdrb
Copy link
Author

andresdrb commented Feb 27, 2022

Yes, using 8.0.3. I think I may have found the issue. I noticed that the PartialRateLimiter was kicking off a lot, so I changed to total rate limiter and the issue seems gone. I have the feeling that because the execution of the request was being delayed, those requests didn't get the AutoTimestamp (maybe they were applied the autotimestamp before de Rate Limit wait), hence when they are finally executed they are "out of sync" of timestamp, so the Autotimestamp should run again after the Rate Limit Wait. Makes sense?

@andresdrb
Copy link
Author

I have a feeling that my assumption is 100% right. My ratelimiter kick in briefly and right there I got a couple of Timestamp errors again

@JKorf
Copy link
Owner

JKorf commented Feb 27, 2022

Right, that makes sense. I'll look into a fix, thanks for the analysis

@andresdrb
Copy link
Author

andresdrb commented Feb 27, 2022 via email

@andresdrb
Copy link
Author

andresdrb commented Feb 27, 2022 via email

@andresdrb
Copy link
Author

Notice how the performance of some of my code degraded dramatically because of the rate limiter since I upgraded to 8.0.3 on friday. It went back to reasonable times today when I changed to the totalRateLimiter, with no errors or failures
image

@JKorf
Copy link
Owner

JKorf commented Feb 27, 2022

Hm intresting, I'll take a look at that. Also I've fixed the issue where timestamps time out when the rate limiter triggers in 8.0.4. Although that might not be an issue for you anymore if it no longer triggers ;)

@andresdrb
Copy link
Author

Yeah for me it is fine as I changed to TotalRateLimiter, but yeah definitelly will help others (It was a bit tricky to find the root cause and there may be people struggling with it out there). Thanks!

@andresdrb
Copy link
Author

Hey, how did you test the fix? I got a burst of timestam errors for 1 hour earlier today after upgrading to 8.0.4...

@JKorf
Copy link
Owner

JKorf commented Feb 28, 2022

Hm weird. I've tested it by adding an await Task.Delay(10000) in the spot where the rate limit would trigger, so simulating a rate limit waiting. That was correctly creating a timestamp after the rate limiting had passed.
Was it happening again after a rate limit? Or during normal process? If you have a lot of time drift, it might be that time syncing once every hour isn't often enough.

@andresdrb
Copy link
Author

andresdrb commented Feb 28, 2022 via email

@JKorf
Copy link
Owner

JKorf commented Feb 28, 2022

Yes, you can set the interval in the client options. But if you have a lot of drift the best solution might be to use a third party app to sync your server time more often with a time provider. People have been using the SP TimeSync tool with success to make sure the the time is correct, which will always be a more reliable way than trying to keep in sync by offsetting the timestamp in this library.

@andresdrb
Copy link
Author

andresdrb commented Feb 28, 2022 via email

@JKorf
Copy link
Owner

JKorf commented Feb 28, 2022

Haha I see. Try using a shorter interval, and let me know if you have any more issues

@andresdrb
Copy link
Author

andresdrb commented Feb 28, 2022 via email

@andresdrb
Copy link
Author

I think there's still some cause-effect somewhere between the Timestamp adjustment and those failures I see. See how on the first image the timestamp is supposed to be adjusted at 1.45PM and right after taht I got a bunch of timestamp errors (when the timestamp was supposed to be adjusted just a moment before). Also it is interesting that it happens mainly on my calls to the GetAccountInfo, just in case that gets you some more insight...

image

image

@JKorf
Copy link
Owner

JKorf commented Feb 28, 2022

Hm well the time syncing is an estimate. It's a simple procedure of recording the local timestamp, requesting the server timestamp and then comparing the 2, and adding the difference to the timestamp sent to the server for authenticated requests. Here's the source for determining the offset:
https://github.com/JKorf/CryptoExchange.Net/blob/4c4cfbb60e0def028c074bb6a7459f019fb1c0ae/CryptoExchange.Net/Clients/RestApiClient.cs#L63

The reason you get the errro mainly on the GetAccountInfoAsync call is because timestamping is only used in authenticated requests.

You're running this on Azure, is time syncing necessary? I would excpect Azure server to have a correct timestamp.

@andresdrb
Copy link
Author

andresdrb commented Feb 28, 2022 via email

@andresdrb
Copy link
Author

andresdrb commented Feb 28, 2022 via email

@JKorf
Copy link
Owner

JKorf commented Feb 28, 2022

Those differences seem way too large to me. 1100ms difference between 6 minutes, thats not right. The question is what is the cause of this..

I've thought of another potential cause of delayed requests.
ServicePointManager.DefaultConnectionLimit
This property determines the max amount of concurrent connections the application can have. It's default 2 or 10 depending on the type of application. If you're doing concurrent requests it might delay requests and mess with the timestamping/time syncing.

Can you try upping this value if you haven't already?

@andresdrb
Copy link
Author

Default connection limit was indeed a problem long ago on my code, but it's been months since that was tuned. This behaviour only started happening since the upgrade to 8.X. Has the way in which HTTP clients are handled changed?

@andresdrb
Copy link
Author

Or any concurrency race condition that you can think of?

@JKorf
Copy link
Owner

JKorf commented Feb 28, 2022

Hm request creating/sending hasnt changed much, rate limiting changed but shouldn't influence it much. Though you could try to turn the rate limiter of completely.
Timesyncing also changed a bit, though it's mostly just the code moved from Binance to the CryptoExchange.Net base library. Obviously since there seems to be something going on with time syncing it's possible something did change and is breaking now.
What exact version were you on where you didnt have issues? I'll look into it some more tomorrow.

@andresdrb
Copy link
Author

I'm going to check on my code for any potential things that may be clogging up connections, but this has not been a problem for long (maybe something has been developing for some time and by coincidence started manifesting itself now).. Can you think of anything that maybe has made the clients more chatty?

@andresdrb
Copy link
Author

andresdrb commented Mar 1, 2022 via email

@JKorf
Copy link
Owner

JKorf commented Mar 1, 2022

I did find another thing, in 7.x there was some functionality that would re-sync the timestamp when a timestamp error was received. This might prevent multiple errors happening for a certain time span since the time should be re-synced when the error is received. I've re-added this functionality in 8.0.5, so that might help you as well.

@andresdrb
Copy link
Author

andresdrb commented Mar 1, 2022 via email

@JKorf
Copy link
Owner

JKorf commented Mar 1, 2022

I know what you mean haha.
All good, let me know if you're having any issues I can help with

@JKorf
Copy link
Owner

JKorf commented Mar 1, 2022

Interesting, this triggers the windows time syncing? Might be a good alternative to a 3rd party program

@andresdrb
Copy link
Author

andresdrb commented Mar 1, 2022 via email

@andresdrb
Copy link
Author

andresdrb commented Mar 2, 2022 via email

@andresdrb
Copy link
Author

I know what you mean haha. All good, let me know if you're having any issues I can help with

It seems I still get the problem every now and then and I don't think it's caused by the high concurrency (right before upgrading to 8.x my code was pretty much the same), so there has to be something else that may have changed, either in the way connections are handled or the timestamp syncing. I just saw that in 8.0.6 you introduced a small change around time syncing. Could that have some effect?

@andresdrb
Copy link
Author

@JKorf I think I have found somethig that has changed since 8.x that is surely impacting this.

I kind of saw the other day in the logs that you are automatically "updating/keeping" trading rules" (so making regular exchangeInfo requests to Binance API) which were not there before. I can see that in the logs of my azure services too, where there are calls being made to the ExchangeInfo endpoint that were not there before. 8.x. IMO you shoul not be automaticaly making those calls (or at least not that frequently) as those are quite expensive and slow and thus more than likely being the ones causing the clogging in outbound requests and so increasing the likelyhood of timestamp missalignment (I know from experience as I was doing a similar thing in the past untill I started caching those on my side with an expiry date of 24 hours).

Notice that I moved to 8.x version by the 25th and that moment is when the exchangeInfo requests spiked

image

Does this make sense?

@andresdrb
Copy link
Author

It seems that for every order placed you are checking the trade rules, where you are triggering the ExchangeInfor call, suuuuuper expensive and slow call. You shouldn't be doing that unless you are applying some caching

image

@andresdrb
Copy link
Author

ok, last one for today today. So I see you have this option to define how often to refresh the trade rules (which you have set by default to 60 minutes, which is way too often IMO) , so I have set it to 24 hours in my code. This all makes sense now why I was seeing those spikes in failures when there was an spike in orders created from my code. This behaviour should be documented somewhere so people is aware of it.

image

image

@JKorf
Copy link
Owner

JKorf commented Mar 4, 2022

@HypsyNZ I agree, which is why it isn't enabled by default. Which raises the question @andresdrb, are you setting the TradeRulesBehaviour property on the client options to anything other than None? Since if it is None (default) it shouldn't be requesting trade rules at all.

@JKorf
Copy link
Owner

JKorf commented Mar 18, 2022

@andresdrb Just checking in, how is it atm? Any improvement?

@andresdrb
Copy link
Author

It happens less, but still happens every now and then. I still feel there's something off that was not happening before...

@devbar
Copy link

devbar commented Mar 24, 2022

Hi guys, I have the same issue. I am on 8.0.9 and everything runs fine on my dev machine. But when I try to deploy I run into the issue instantly.

I tested this on two different machines. I synced time several times, no way out :(

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
@devbar @JKorf @andresdrb and others