@@ -53,6 +53,9 @@ inline constexpr auto kRecordSettle=std::chrono::milliseconds(250);
5353// registering its unit with the generation's owner. One bound for both, so a
5454// registration retried within it never outlives the wait for it.
5555inline constexpr auto kStartupGrace =std::chrono::seconds(10 );
56+ // A record that exists and is still refused after kRecordSettle. The refusal
57+ // is a fact about the moment, not about the record's content.
58+ inline constexpr std::string_view kRecordUnreadable =" cannot read compiler transaction record" ;
5659enum class Opened { yes, absent, refused };
5760// Opens `path' for reading, waiting out a publication in progress.
5861Opened open_settled (std::ifstream& input,const fs::path& path) {
@@ -76,10 +79,10 @@ Record read(const fs::path& path, std::string_view tag) {
7679 std::ifstream input;
7780 const auto opened=open_settled (input,path);
7881 if (opened==Opened::absent)return std::optional<Fields>{};
79- if (opened==Opened::refused)return std::unexpected (" cannot read compiler transaction record " );
82+ if (opened==Opened::refused)return std::unexpected (std::string ( kRecordUnreadable ) );
8083 std::string bytes (65537 ,' \0 ' );
8184 input.read (bytes.data (),bytes.size ());bytes.resize (static_cast <std::size_t >(input.gcount ()));
82- if (input.bad ())return std::unexpected (" cannot read compiler transaction record " );
85+ if (input.bad ())return std::unexpected (std::string ( kRecordUnreadable ) );
8386 if (bytes.size () < 18 || bytes.size () > 65536 || bytes[bytes.size ()-18 ] != ' \n ' || bytes.back () != ' \n ' )
8487 return std::unexpected (" corrupt compiler transaction record" );
8588 const auto body = std::string_view (bytes).substr (0 ,bytes.size ()-18 );
@@ -438,6 +441,18 @@ private:
438441 std::atomic<bool > stopping_{false },failed_{false };
439442 std::mutex admission_;
440443 ObserverMap held_;
444+ // AN UNREADABLE RECORD IS ASKED AGAIN AT THE NEXT AUDIT, as an unknown
445+ // controller is. A Windows reader can be refused a just-published record
446+ // for longer than kRecordSettle while the host scans new files; the
447+ // refusal decides the loss of a compiler only when it outlasts the
448+ // startup grace. Corrupt content is decided at once.
449+ std::map<std::string,std::chrono::steady_clock::time_point> unreadable_;
450+ bool settling (const std::string& attempt,std::string_view error) {
451+ if (error!=kRecordUnreadable )return false ;
452+ const auto now=std::chrono::steady_clock::now ();
453+ const auto first=unreadable_.try_emplace (attempt,now).first ->second ;
454+ return now-first<kStartupGrace ;
455+ }
441456 void fail (std::string_view reason) {
442457 if (!failed_.exchange (true ))
443458 std::println (std::cerr," mcpp: compiler witness: {}" ,reason);
@@ -446,20 +461,21 @@ private:
446461 for (auto it=held_.begin ();it!=held_.end ();) {
447462 const auto directory=named (it->first );
448463 auto retired=read (directory/" retired" ," mcpp-retired-v1" );
449- if (!retired) {fail (retired.error ());++it;continue ;}
464+ if (!retired) {if (! settling (it-> first ,retired. error ())) fail (retired.error ());++it;continue ;}
450465 if (*retired&&(**retired).size ()!=3 ) {fail (" invalid retirement certificate" );++it;continue ;}
451- if (*retired) {it=held_.erase (it);continue ;}
466+ if (*retired) {unreadable_. erase (it-> first ); it=held_.erase (it);continue ;}
452467 auto attempt=Attempt::load (directory);
453- if (!attempt) {fail (attempt.error ());++it;continue ;}
468+ if (!attempt) {if (!settling (it->first ,attempt.error ()))fail (attempt.error ());++it;continue ;}
469+ unreadable_.erase (it->first );
454470 const auto state=controller_state (*attempt);
455471 if (state==Controller::unknown) {++it;continue ;} // asked again at the next audit
456472 if (state==Controller::ended) {
457473 // Retirement can be published between the first read and the
458474 // controller's exit. Observe it again before reporting loss.
459475 retired=read (directory/" retired" ," mcpp-retired-v1" );
460- if (!retired)fail (retired.error ());
476+ if (!retired) { if (! settling (it-> first ,retired. error ())) fail (retired.error ());}
461477 else if (*retired&&(**retired).size ()!=3 )fail (" invalid retirement certificate" );
462- else if (*retired) {it=held_.erase (it);continue ;}
478+ else if (*retired) {unreadable_. erase (it-> first ); it=held_.erase (it);continue ;}
463479 else fail (" controller ended without retirement certificate" );
464480 }
465481 ++it;
0 commit comments