WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit f20b631

Browse files
Fixing race condition during token generation (#5306)
* Fixing race condition during token generation * Correcting changes * Fixing function name and error handling * Minor fixes * Restructuring the UpdateStateOnServer function
1 parent 129085f commit f20b631

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Agent.Worker/ExecutionContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,15 @@ public IExecutionContext CreateChild(Guid recordId, string displayName, string r
268268
return child;
269269
}
270270

271+
271272
public void Start(string currentOperation = null)
272273
{
273274
_record.CurrentOperation = currentOperation ?? _record.CurrentOperation;
274275
_record.StartTime = DateTime.UtcNow;
275276
_record.State = TimelineRecordState.InProgress;
276277

277-
_jobServerQueue.QueueTimelineRecordUpdate(_mainTimelineId, _record);
278+
//update the state immediately on server
279+
_jobServerQueue.UpdateStateOnServer(_mainTimelineId, _record);
278280

279281
if (_logsStreamingOptions.HasFlag(LogsStreamingOptions.StreamToFiles))
280282
{
@@ -819,7 +821,8 @@ private void InitializeTimelineRecord(Guid timelineId, Guid timelineRecordId, Gu
819821
_record.WorkerName = configuration.GetSettings().AgentName;
820822
_record.Variables.Add(TaskWellKnownItems.AgentVersionTimelineVariable, BuildConstants.AgentPackage.Version);
821823

822-
_jobServerQueue.QueueTimelineRecordUpdate(_mainTimelineId, _record);
824+
//update the state immediately on server
825+
_jobServerQueue.UpdateStateOnServer(_mainTimelineId, _record);
823826
}
824827

825828
private void JobServerQueueThrottling_EventReceived(object sender, ThrottlingEventArgs data)

src/Microsoft.VisualStudio.Services.Agent/JobServerQueue.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public interface IJobServerQueue : IAgentService, IThrottlingReporter
2222
bool ForceDrainTimelineQueue { get; set; }
2323
event EventHandler<ThrottlingEventArgs> JobServerQueueThrottling;
2424
Task ShutdownAsync();
25+
Task SendTimelineRecordUpdateAsync(Guid timelineId, TimelineRecord timelineRecord);
2526
void Start(Pipelines.AgentJobRequestMessage jobRequest);
2627
void QueueWebConsoleLine(Guid stepRecordId, string line, long lineNumber);
2728
void QueueFileUpload(Guid timelineId, Guid timelineRecordId, string type, string name, string path, bool deleteSource);
2829
void QueueTimelineRecordUpdate(Guid timelineId, TimelineRecord timelineRecord);
30+
void UpdateStateOnServer(Guid timelineId, TimelineRecord timelineRecord);
2931
void UpdateWebConsoleLineRate(Int32 rateInMillis);
3032
}
3133

@@ -232,6 +234,42 @@ public void QueueTimelineRecordUpdate(Guid timelineId, TimelineRecord timelineRe
232234
Trace.Verbose(StringUtil.Format("Enqueue timeline {0} update queue: {1}", timelineId, timelineRecord.Id));
233235
_timelineUpdateQueue[timelineId].Enqueue(timelineRecord.Clone());
234236
}
237+
public void UpdateStateOnServer(Guid timelineId, TimelineRecord timelineRecord)
238+
{
239+
ArgUtil.NotEmpty(timelineId, nameof(timelineId));
240+
ArgUtil.NotNull(timelineRecord, nameof(timelineRecord));
241+
ArgUtil.NotEmpty(timelineRecord.Id, nameof(timelineRecord.Id));
242+
243+
//sending immediate server update for the job timeline records to server
244+
if (string.Equals(timelineRecord.RecordType, "Job", StringComparison.OrdinalIgnoreCase))
245+
{
246+
try
247+
{
248+
// Attempting to send immediate update for job records
249+
SendTimelineRecordUpdateAsync(timelineId, timelineRecord).GetAwaiter().GetResult();
250+
}
251+
catch (Exception ex)
252+
{
253+
Trace.Warning($"Failed to send immediate timeline record update: {ex.Message}. Falling back to queue mechanism.");
254+
QueueTimelineRecordUpdate(timelineId, timelineRecord);
255+
}
256+
}
257+
else
258+
{
259+
// All other record types use queue mechanism
260+
QueueTimelineRecordUpdate(timelineId, timelineRecord);
261+
Trace.Verbose($"Timeline record {timelineRecord.Id} queued for update (RecordType: {timelineRecord.RecordType})");
262+
}
263+
}
264+
public async Task SendTimelineRecordUpdateAsync(Guid timelineId, TimelineRecord timelineRecord)
265+
{
266+
ArgUtil.NotEmpty(timelineId, nameof(timelineId));
267+
ArgUtil.NotNull(timelineRecord, nameof(timelineRecord));
268+
ArgUtil.NotEmpty(timelineRecord.Id, nameof(timelineRecord.Id));
269+
var jobtimelinerecord = new List<TimelineRecord> { timelineRecord.Clone() };
270+
await _jobServer.UpdateTimelineRecordsAsync(_scopeIdentifier, _hubName, _planId, timelineId, jobtimelinerecord, CancellationToken.None);
271+
Trace.Info($"Job timeline record {timelineRecord.Id} sent successfully to server");
272+
}
235273

236274
public void ReportThrottling(TimeSpan delay, DateTime expiration)
237275
{

0 commit comments

Comments
 (0)